Skip to content

Instantly share code, notes, and snippets.

@screamingmunch
Last active December 19, 2015 21:39
Show Gist options
  • Save screamingmunch/6022169 to your computer and use it in GitHub Desktop.
Save screamingmunch/6022169 to your computer and use it in GitHub Desktop.
Devise, Heroku

Always setup your layouts in your rails app/views/layouts/application.html.erb file

when you want to create a seperate layout different from the rest (say, a splash page), create a new layout file and introduce a different yield.

app/views/layouts/spalsh.html.erb.

http://stackoverflow.com/questions/11805214/how-to-switch-out-rails-layouts

<!Doctype>
<head>
</head>
<yield 1>
<yield 2>
<end>

RAILS_ENV=production rails s

##Devise

https://github.com/jherrlin/ClassNotes/blob/master/wk5d3/devise.md

Step 1: Install devise

  • put this in your gemfile gem 'devise'
    • run bundle
    • now run rails generate devise:install

Step 2: Create your user model
run rails generate devise MODEL where MODEL is the name of the model you want (i.e. User)

Step 3: Migrate your database
rake db:migrate

Step 4: Restart your rails server (if it is started)
Devise will add routes to your routes file for your new user model.

Step 5: Block users from using site without loggin in.
Add before_filter :authenticate_user! to your application controller.
If you only want to block certain parts of the site you can add this to individual controllers.

Step 6: Create views
You can manually create these, or generate them from devise
rails generate devise:views will generate views for you

Vist your site and sign_up/login!

Some awesome methods you now can use:

  • user_signed_in? this checks to see if a user is signed_in
  • current_user this returns the user object for the user that is logged in.
  • user_session this returns a session object

####Adding an Admin model

Now, what if we want to add another model for Admins on top of our User model?

Step 1: Create your admin model
rails generate devise Admin

Step 2: Migrate your database (again) and restart your server (again)
rake db:migrate

Step 3: Create more views
run rails generate devise:views admin one more time, and this will create your admin views

Step 4: Block regular users from seeing admin pages
put before_filter :authenticate_admin! in any controllers you wish to be admin only.

  • to block other users from using Admin account.. add more validations.

http://railscasts.com/episodes/235-devise-and-omniauth-revised

If you've already had a 'user' model: http://stackoverflow.com/questions/15109781/step-on-how-to-add-devise-migration-to-existing-user-model-in-ruby-on-rails

##Deploying to Heroku

https://devcenter.heroku.com/articles/git#creating-a-heroku-remote

Make sure your git is up-to-date

$ git status
$ git add .
$ git commit -m "most recent commit"

$ heroku create  # creates a new app on heroku, along w/ a git remote that must be used to receive your app source

$ git remote -v  # verifie the remote in your git config

$ heroku git:remote -a 
 
#DEPLOY YOUR CODE:

$ git push heroku master

if you get the following error:
fatal: 'heroku' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
$ heroku keys:add ~/.ssh/id_rsa.pub 

It should give you "Uploading SSH public key /home/renee/.ssh/id_rsa.pub... done"

then go ahead and run "git push heroku master" again.

If you have database, and seed data, make sure to run these:

$ heroku run rake db:migrate
$ heroku run rake db:seed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment