views:

21

answers:

1

my routes should look like this:

    >rake routes

                      GET    /categories/:category_id/articles(.:format)          {:controller=>"articles", :action=>"index"}
    category_articles POST   /categories/:category_id/articles(.:format)          {:controller=>"articles", :action=>"create"}
 new_category_article GET    /categories/:category_id/articles/new(.:format)      {:controller=>"articles", :action=>"new"}
                      GET    /categories/:category_id/articles/:id(.:format)      {:controller=>"articles", :action=>"show"}
                      PUT    /categories/:category_id/articles/:id(.:format)      {:controller=>"articles", :action=>"update"}
     category_article DELETE /categories/:category_id/articles/:id(.:format)      {:controller=>"articles", :action=>"destroy"}
edit_category_article GET    /categories/:category_id/articles/:id/edit(.:format) {:controller=>"articles", :action=>"edit"}
                      GET    /categories(.:format)                                {:controller=>"categories", :action=>"index"}
           categories POST   /categories(.:format)                                {:controller=>"categories", :action=>"create"}
         new_category GET    /categories/new(.:format)                            {:controller=>"categories", :action=>"new"}
                      GET    /categories/:id(.:format)                            {:controller=>"categories", :action=>"show"}
                      PUT    /categories/:id(.:format)                            {:controller=>"categories", :action=>"update"}
             category DELETE /categories/:id(.:format)                            {:controller=>"categories", :action=>"destroy"}
        edit_category GET    /categories/:id/edit(.:format)                       {:controller=>"categories", :action=>"edit"}

but whenever I try, for articles, to use anything other than new_category_article_path,

<%= link_to 'Show', categories_article %>

I get this mean exception:

undefined local variable or method 'categories_article' for #<#<Class:0x00000102ce5768>:0x00000102ce3a80>

What is it I am doing wrong here?

+2  A: 

A couple things going on here:

  1. There's no route defined that's named "categories_article". You might be looking for "category_articles" or "category_article".
  2. For a "show" action, you need to specify the ID of an object to display. In this case, you also need to specify the parent id (category_id)
  3. When referring to these routes by name, you need to add "_path" or "_url" at the end.

These three together makes something like:

<%= link_to 'Show', category_articles_path(:category_id => category.id) %>

or

<%= link_to 'Show', category_article_path(:category_id => category.id, :id => article.id) %>

or, shorter:

<%= link_to 'Show', [category, article] %>
vegetables
Thanks a lot, mattyven!
Jan Limpens
Just one more thing - is there a syntax to do the shortest version for other actions, such as edit?
Jan Limpens
<%= link_to 'Show', [a.category, a] %> gives me the same result as <%= link_to 'Edit', [a.category, a], :action => "edit"%>, which I looked up from the docs
Jan Limpens
For edit, I would suggest using the "edit_category_article_path(category_id, article_id)" way of doing things
vegetables
got it, thanks!
Jan Limpens