Ruby on Rails - setup for nested form in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Setup for nested form in ruby on rails
The first to thing to have: a model that contains a has_many relation with another model.
In ProjectsController:
In a nested form, you can create child objects with a parent object at the same time.
As we initialized @project with Project.new to have something for creating a new Project object, same way for creating a Todo object, we have to have something like this, and there are multiple ways to do so:
- In Projectscontroller, in new method, you can write: @todo = @project.todos.build or @todo = @project.todos.new to instantiate a new Todo object.
- You can also do this in view: <%= f.fields_for :todos, @project.todos.build %>
For strong params, you can include them in the following way:
Since, the Todo objects will be created through the creation of a Project object, so you have to specify this thing in Project model by adding the following line: