rails user models

Ashley Williams, NYCDA

Tuesday, 23 April 2013

Today's goals


  • Create the model for projects on your personal site
  • Create the models for your personal site's admin user
  • Relate your projects model to your user model
  • Restrict creation, deletion, and editing, of projects to logged in users

user model


  • we need to add a password field to our user model

  1. add password_digest, password and password_confirmation to our Users table
  2. add bcrypt-ruby to our Gemfile


user model


  • password and password confirmation should have attar_accessible and should be in the form
  • we should validate that the password is longer than 6 characters, and that there is a password confirmation
  • rails method: has_secure_password (as long as there is a password_digest column in the database, adding this one method to our model gives us a secure way to create and authenticate new users)

custom routes


  • we can use the match keyword to create new custom routes that don't match action names

match '/custom_route_name', to: 'model#action'

E.G> match '/signup', to: 'users#new"


test this by doing 'rake routes'

form_for


  • To create a form, we can use the Rails helper "form_for"

<%= form_for(object) do |f| %>
      <%= f.label :attribute %>
      <%= f.data_input :attribute %>
<%= f.submit "Label" %>

signup failure


  • we need to check if the @user is saved (i.e. passes validation)
  • if they are saved we should go to the next step
  • if they aren't we need to render the new page again


what action in the controller should we use for this?

error messages


object_name.errors.full_messages is an object that holds an array of error message

we can display these using a loop. what sort of loop do you think we should use?

display error messages


<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      The form contains <%= pluralize(@user.errors.count, "error") %>.
    </div>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li>* <%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

partials


  • A partial is a reusable piece of code that you'd want to use in multiple views
  • you should save partials in a folder inside views called "shared"
  • we save them with an underscore before the name e.g. "_my-partial.html.erb"
  • to add a partial to a page we simple write:
  • <%= render 'shared/error_messages' %>

signup success


we want to redirect users who successfully sign up to their new user page

we can do this in the users_controller using the keyword redirect_to

what is the path to that particular user's user page?

flash


we want to tell the user that they successfully signed up

we can do this with a message called "flash"

flash is a variable that can hold different values with different names (keys)

      <% flash.each do |key, value| %>
        <div class="alert alert-<%= key %>"><%= value %></div>
      <% end %>

flash


we need to add the flash to our controller create action:

flash[:success] = "Welcome to the Sample App!"

rails user models

By ag_dubs

rails user models

  • 602