Course: Ruby on Rails » Forms and Authentication » Project: Forms
##Objective
These projects will give you a chance to actually build some forms, both using nearly-pure HTML and then graduating to using the helper methods that Rails provides.
Disclaimer: This is my implementation of Project:Forms from the Odin Project. I'm no Rails Master, so do not take this walkthrough as gospel. If you see any errors or something that is incorrect, please feel free to contact me in the comments.
##Step 1: Setup
First, create a basic Rails app with User Model and Controller.
From the command line, create a new Rails app:
Create and Migrate User Model:
In app/models/user.rb, add validations for presence:
In config/routes.rb, create routes for #new and #create actions:
In console, check to make sure routes were created:
Generate a UsersController:
In app/controllers/users_controller.rb, write empty methods for #new and #create:
In app/views/users folder, create new.html.erb file. Add boilerplate text to file.
From the command line, load development server using rails s. You should see the boilerplate text at ` http://localhost:3000/users/new`.
##Step 2: Build HTML Form
Build a form for creating a new user:
If you try to submit data now you will receive an ActionController::InvalidAuthenticityToken error. Add authenticity token as follows:
Now if you try to submit data, you will get a template is missing error.
That is okay. This means we’ve reached our #create action in the controller and by default it looks for an app/views/users/create.html.erb file.
Instead, lets build the #create action to go elsewhere:
In app/controllers/users_controller.rb, create and implement a #user_params helper method:
Update html form to submit hash of user parameters:
Update #create action with new helper method:
Finally, confirm you can submit data using the form:
Log output:
##Step 3: Build #form_tag Form
In this step, we will replace our html form with a #form_tag Form.
First, comment out your html form:
Switch to using #form_tag and #*_tag helper methods. Note that Rails will insert authenticity token automatically.
In app/controllers/user_controller.rb, modify#create method to once again accept normal top level User attributes.
Confirm you can submit data.
##Step 4: Build #form_for Form
Modify your #new action in the controller to instantiate a blank User object and store it in an instance variable called @user.
Comment out #form_tag and build #form_for form:
Note: I’ve added a default placeholder value for :email.
Finally, in app/controllers/user_controller.rb, switch your controller’s #create method to accept the nested :user hash from params.
Confirm you can submit data using the newly created #form_for form:
##Step 5: Editing Users
Update your routes and controller to handle editing an existing user.
In config/routes.rb:
In app/controllers/users_controller.rb:
Create app/views/users/edit.html.erb. Copy/paste your form from the New view to Edit form:
“View source” on the form generated by #form_for in your Edit view.
You should see authentication token and other relevant hidden fields.
Confirm that you can submit data and that validations are working.
##Step 6: Extra Credit
In app/views/users/new.html.erb and app/views/users/edit.html.erb, include error messages:
Now, when validations fail, errors should display:
Congratulations! You’ve just created basic forms using html and ruby helper methods.