gem 'devise'to your Gemfile and
rails g devise:install
rake db:migrateand restart your server.
current_userhelper method, available within all views and controllers, that will retrieve the row from the Users table for whoever is currently signed in
before_action :authenticate_user!filter that we can use in our controllers to ensure someone is signed in before accessing any actions within that controller
rails g starter:style defaultincludes 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
ApplicationControllerto 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.
devise, with a whole bunch of stuff in it. What we are most interested in are the contents of the
app/views/devise/registrations/new.html.erbis the sign up form,
registrations/edit.html.erbis the edit profile form, and
sessions/new.html.erbis the sign in form.
<input>elements in its forms, so we don't directly see those things in the view templates.
form_forhelper method is spitting out the
<form>tag, and each of the
f.____fieldmethods are spitting out the
class="form-group"to each of those. Also, we should remove the
<br />tags that Devise includes.
____fieldhelpers; the most common are
f.collection_select-- this one is special. It is similar to the
options_from_collection_for_selecthelpers 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.
ApplicationControllerand add the following code: