form_for Lab
Objectives
Use
form_forto build anewformUse
form_forto build aneditformMass assign form data to
ActiveRecordinstances onnewandeditBuild RESTFul actions for
new,create,edit, andupdate, with the correct routes and route helper urls.
Instructions
For this lab you will be extending the functionality of the Flatiron school's student management application by building in the functionality to create and edit school classes. You will build all of the functionality from scratch, including building out the models, views, and controllers for the SchoolClass and Student resources.
Below is what will be needed for each component of the MVC structure:
Models - Create a
SchoolClassmodel, along with its associated database table. TheSchoolClasstable should have the columns:title:stringandroom_number:integer. TheStudentmodel and database table have already been created for you.Controllers - Create controller files for
school_classesandstudents. Build out thenew,create,show,edit, andupdateactions in each of the controllers.Views - Create a show page for each resource, along with
newandeditforms.
You will also need to create routes for each path mentioned above.
The tests for this application are located in the spec/controllers/, spec/features/, and spec/models/ directories.
Requirements
Utilize the form_for methods for all four of the forms and integrate each form_for call with the respective controller actions. Also make sure to utilize the <%= f.submit %> submit button syntax to have the button text automatically generated.
Since you're using form_for, make sure you are using strong parameters for the controller create and update actions, if you don't remember how to do it, here is how we implemented strong params in the README: @post.update(params.require(:post).permit(:title, :description)). (hint: you can pass the strong parameter call to the create method just like we did on update)
If you run into issues drawing the routes, inspect element on the forms to see what path form_for is automatically trying to pass the form data to.
Resources
Clone: https://github.com/learn-co-curriculum/rails-form_for-lab
Last updated