Question about link_to helper

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.

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:

already assigned to an issue?

Is that clear enough?

Thanks.


Sincerely,

William P.

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs