views:

30

answers:

1

I have two MVCs Meeting and Agenda. A meeting can have many agenda items.

I am trying to set these two pages up so that I can click a 'Create New Agenda Item' on the show.html.rb meeting page. The idea being that this would redirect the user to the new.html.rb agenda page passing the meeting id, and pre-populating the new.html.rb agenda form with the meeting ID.

Currently I have the following which passes the meeting ID in the URL but I have no idea how to pass it to the form.

Meeting: show.html.rb:

<%= link_to "Add Agenda Item", :action => 'new_agenda', :id => @meeting.id %>

Go gentle, I'm very new. But many thanks in advance.

Christian Macedo.

A: 

The new action within your Agenda controller should have access to the meeting ID query parameter. Something like:

def new
  @agenda = Agenda.new
  @agenda.meetings.build(Meeting.find(params[:id]))
end
John Topley
Ok, I tried adding this, however I now get an error when clicking the link for a new agenda item.You have a nil object when you didn't expect it!The error occurred while evaluating nil.buildEven if this weren't the case. I dont understand how the second line of code is obtaining the id of the meeting that I've passed in the URL. I think I am very much missing something here. Or just plain old practice.
Christian
Try changing it to `@agenda.meeting = Meeting.find(params[:id])`. The meeting ID is passed in the `params` hash. See here for details: http://guides.rubyonrails.org/routing.html#querystring-parameters
John Topley
That won't work, because params[:id] would give you the Agenda's id if you were in the new Agenda view. If Agenda is a nested resource of Meeting, you can do params[:meeting_id] though. Also, @agenda.meetings doesn't make sense because Meetings have many Agendas, not the other way around.
Karl
I'm going to take that back. If you passed it into the new Agenda view as :id, you should be able to access it as params[:id].
Karl