I have an object title
with an association song
In my index of a titles, each title
has a link_to
button that creates a new title
that I would like associated with the same song
object. So it's something like this:
link_to "Add Title", new_title_path, remote: true
In the view that has the above code I have the song_id
. I can display the form that gets returned when this link is clicked, I can submit the data and create the new title
but how do I get the song_id
into that form or into the new title
?
Add parameter to the link, assuming song
is the object
link_to "Add Title", new_title_path(song_id: @title.song_id), remote: true
Now in the new action
def new
@title = Title.new(song_id: params[:song_id])
end
and finally in the new.html.erb
form, add a hidden field tag
<%= f.hidden_field :song_id, @title.song_id %>
Hope this helps!