I’m working on my first Rails app. It’s a redesign for a magazine
site. I have a class for the articles and a class for issues. I’ve
set up a has_many/belongs_to relationship between the two so that
every article belongs to a particular issue.
Now I’d like to set it up so that a new article cannot be added UNLESS
you have already chosen the issue that it is to be added to. I’d like
to set something up like
“selected_issue.articles.build(params[:article])” but I don’t know how
to create an action/method that would allow me to set the
selected_issue variable and then pass that into the create method on
the article controller.
you got it right. you can call @issue.articles.create( params[:article] ) to get a new article that
is already saved and associated with the issue. you can also do @issue.articles.build( params[:article] ) if you want to get a new
article object but not save it yet.
to set something up like
“selected_issue.articles.build(params[:article])” but I don’t know how
to create an action/method that would allow me to set the
selected_issue variable and then pass that into the create method on
the article controller.
A common way to do this is for your “create article” form to have a
select field for issue_id that allows the user to select from
available issues.