开发者

How should these rails paths look like?

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?


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] %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜