gem 'devise'
to your Gemfile and bundle
rails g devise:install
config/routes.rb
:This is just a shortcut for setting a root URL the old way,1get "/", :controller => "photos", :action => "index"Copied!
rake db:migrate
and restart your server.current_user
helper method, available within all views and controllers, that will retrieve the row from the Users table for whoever is currently signed inbefore_action :authenticate_user!
filter that we can use in our controllers to ensure someone is signed in before accessing any actions within that controllerrails g starter:style default
includes examples in the nav bar of what the sign-up/in/out links look like. You'll have to customize them a bit based on what you called your secure model. Notice the data-method="delete"
attribute on the sign-out link -- that is important, and you need to include it. (But be careful not to include it on regular links if you, e.g., copy-paste this one.)before_action :authenticate_user!
in our ApplicationController
to force visitors to sign up or sign in before doing anything else, then the sign in page is our landing page, after all (try going to Twitter, Facebook, etc when signed out -- the landing page is really a sign-in page with some extra info thrown in). So it would be nice to be able to make it pretty.app/views
folder called devise
, with a whole bunch of stuff in it. What we are most interested in are the contents of the registrations
and sessions
subfolders. app/views/devise/registrations/new.html.erb
is the sign up form, registrations/edit.html.erb
is the edit profile form, and sessions/new.html.erb
is the sign in form.<form>
and <input>
elements in its forms, so we don't directly see those things in the view templates.form_for
helper method is spitting out the <form>
tag, and each of the f.____field
methods are spitting out the <input>
tags.<div>
.class="form-group"
to each of those. Also, we should remove the <br />
tags that Devise includes.____field
helpers; the most common aref.text_field
f.email_field
f.password_field
f.number_field
f.check_box
f.collection_select
-- this one is special. It is similar to the select_tag
and options_from_collection_for_select
helpers we discussed in class, but rolled into one. The syntax looks like this (assuming you have a Company table and you want the user to belong to one company):app/views/devise/
folder to use as a starting point, if you wish.ApplicationController
and add the following code: