I am trying to create nested routes for a project that has many boards.
First I am creating a link to a new board:
<%= link_to 'New Board', new_project_board_path(@project) %>
In routes.rb I just nested the restful routes:
resources :projects do
resources :boards
end
And in the boards controller I adapted the new and create action as follows:
def new
@project = Project.find(params[:project_id])
@board = @project.boards.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @board }
end
end
def create
@project = Project.find(params[:project_id])
@board = @project.boards.new(params[:board])
respond_to do |format|
if @board.save
format.html { redirect_to @board, notice: 'Board was successfully created.' }
format.json { render json: @board, status: :created, location: @board }
else
format.html { render action: "new" }
format.json { render json: @board.errors, status: :unprocessable_entity }
end
end
end
At least I also changed the _form.html.erb into:
<%= form_for([@project, @board]) do |f| %>
etc.
My problem starts when I click the button create board (so the new action is executed) it says no route matches. I guess it has to do with the create action because it doesn’t get the id of the project anymore? I really don’t know what else to change and where.
Seems your error is here
You don’t have non-nested :boards resources, do you? Try to change this line to