I am tweaking "getting started" tutorial and trying to create an instance of a dependant model.
My models are Museum and Exhibition (Article and Comment in the tutorial).
The tutorial teaches how to create an instance of Comment when located in the Artcicle#show ERB file. This is achieved by using the following form helper:
<%= form_for([@article, @article.comments.build]) do |f| %>
I guess this form helper allows the form to access the Article Id and therefore allows accurate dependency.
I have managed to do that with my own models Museum and Exhibition.
Though I would like to be able to create an Exhibition instance without being located in the Museum#Show ERB file. But directly inside the Exhibition#New ERB file.
I have realised there was no route initially to do that, then have declared Exhibition as a proper ressource, on top of being dependant to Museum. Here my Routes file now :
resources :exhibitions
resources :museums
resources :museums do
resources :exhibitions
end
which have created appropriate routes.
Though I am struggling with the Exhibition#New controller and ERB files.
Intuitively I understand I need to pass a list of all museums instances Ids and Names so that the user can choose to which Museum the exhibition belongs to.
But all my attempts to create the form helper have failed so far. My lastattempt was as such :
<%= form_for([@museum, @museum.exhibitions.build]) do |f| %>
<p>
<%= f.select @museum.id, options_for_select(@museum.name) %> </p>
....
with the following Exhibition controller bit :
def new
@museum = Museum.all
end