Rails foundationSort Order

Rails: User Registration

In this episode we implement the basic user registration controller and view.

Let's begin by creating the user controller

rails g controller users

We should now see the users_controller.rb in our controller directory.

class UsersController < ApplicationController  def new    @user = User.new  end    def create    @user = User.new(user_params)    if @user.save      redirect_to root_path    else      # handle error    end  end    private    def user_params    params.require(:user).permit(:email, :password, :password_confirmation)  endend

We also need to create the view to go with this controller

# app/views/users/new.html.erb<%= form_for @user do |f| %>  <div class='form-group'>    <%= f.label :email %>    <%= f.text_field :email, class: 'form-control' %>  </div>  <div class='form-group'>    <%= f.label :password %>    <%= f.password_field :password, class: 'form-control' %>  </div>  <div class='form-group'>    <%= f.label :password_confirmation %>    <%= f.password_field :password_confirmation, class: 'form-control' %>  </div>  <div class='actions'>    <%= f.submit 'Finish', class: 'btn btn-success btn-block' %>  </div><% end %>

We will also need to update the config/routes.rb

Rails.application.routes.draw do  resources :users    # ...    root to: 'posts#index'end

That should be it in terms of the setup. We should now be able to create a user using the form.