Hi all, I was wondering if someone might be able to answer a question for me. I''m working on a re-design of a magazine site and have models for both issue and article. Every article needs to belong to an issue, I''ve already set up the has_many/belongs_to relationship in the models. What I''d like to know is if I can pass the ID of an issue to the new action for the article so that I can use something like issue.articles.build when creating the new article so that it is already assigned to an issue? Is that clear enough? Thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Assuming that @issue is set to the current issue and the article controller is called article and the action is called new, this works: <%= link_to ''New Article'', :controller => ''article'', :action => ''new'', :issue_id => @issue %> For issue id 6 this would generate "/article/new?issue_id=6 " skoppy wrote:> Hi all, > > I was wondering if someone might be able to answer a question for me. > I''m working on a re-design of a magazine site and have models for both > issue and article. Every article needs to belong to an issue, I''ve > already set up the has_many/belongs_to relationship in the models. > > What I''d like to know is if I can pass the ID of an issue to the new > action for the article so that I can use something like > issue.articles.build when creating the new article so that it is > already assigned to an issue? > > Is that clear enough? > > Thanks. > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
if you are doing it restfully you could nest the resouces Routes.rb: map.resources :issues do |issues| issues.resources :articles end Then in your view you would do: link_to new_article_path(@issue) And finally in your controller define a before filter like this: before_filter :get_issue def get_issue @issue = Issue.find(params[:issue_id] end On Sep 24, 4:16 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> Assuming that @issue is set to the current issue and the article > controller is called article and the action is called new, this works: > > <%= link_to ''New Article'', :controller => ''article'', :action => ''new'', > :issue_id => @issue %> > > For issue id 6 this would generate "/article/new?issue_id=6 " > > > > skoppy wrote: > > Hi all, > > > I was wondering if someone might be able to answer a question for me. > > I''m working on a re-design of a magazine site and have models for both > > issue and article. Every article needs to belong to an issue, I''ve > > already set up the has_many/belongs_to relationship in the models. > > > What I''d like to know is if I can pass the ID of an issue to the new > > action for the article so that I can use something like > > issue.articles.build when creating the new article so that it is > > already assigned to an issue? > > > Is that clear enough? > > > Thanks. > > -- > Sincerely, > > William Pratt--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for all the help guys. I haven''t tackled the whole restful thing yet Andrew but will keep your advice in mind for when I do. Thanks again. On Sep 24, 5:58 pm, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> if you are doing it restfully you could nest the resouces > > Routes.rb: > map.resources :issues do |issues| > issues.resources :articles > end > > Then in your view you would do: > link_to new_article_path(@issue) > > And finally in your controller define a before filter like this: > before_filter :get_issue > > def get_issue > @issue = Issue.find(params[:issue_id] > end > > On Sep 24, 4:16 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > > > Assuming that @issue is set to the current issue and the article > > controller is called article and the action is called new, this works: > > > <%= link_to ''New Article'', :controller => ''article'', :action => ''new'', > > :issue_id => @issue %> > > > For issue id 6 this would generate "/article/new?issue_id=6 " > > > skoppy wrote: > > > Hi all, > > > > I was wondering if someone might be able to answer a question for me. > > > I''m working on a re-design of a magazine site and have models for both > > > issue and article. Every article needs to belong to an issue, I''ve > > > already set up the has_many/belongs_to relationship in the models. > > > > What I''d like to know is if I can pass the ID of an issue to the new > > > action for the article so that I can use something like > > > issue.articles.build when creating the new article so that it is > > > already assigned to an issue? > > > > Is that clear enough? > > > > Thanks. > > > -- > > Sincerely, > > > William Pratt--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---