开发者

Router for nested resources in a "not usual" Ruby on Rails way

I am using Ruby on Rails 3.0.7 and I am trying to set nested resource routing to make it to work in a "not regular" RoR way.

In my routes.rb file I have

resources :articles do
  resources :categories, :only => [:index], :controller => 'articles/categories' # The related controller is Articles::CategoriesController
end

so that I can browse following URLs:

<my_site>/articles/1/categories
<my_site>/articles/2/categories
...

What I would to do is to access new, 开发者_JAVA技巧edit and show controller actions for categories by using the same articles/categories controller used for the nested resource stated above (that is, Articles::CategoriesController) and by accessing these URLs:

<my_site>/articles/categories/new
<my_site>/articles/categories/edit
<my_site>/articles/categories/1
<my_site>/articles/categories/2
...

How can I do that? How I must code the router?


Maybe I can do something by using the router collection method like this

resources :articles do
  collection do
   # match something here for the Articles::CategoriesController...
  end

  resources :categories, :only => [:index], :controller => 'articles/categories'
end

but I don't know how to do that.


I'm not real sure what you're trying to do with those routes, so I'm not quite sure how to answer your questions. If your intent is to be able to add a new category for a particular article, or edit all the categories for a particular article, you have to pass an ID for the article. If you're trying to create a new article and a new category all at once, you don't need category in the route, just the article and you can do something like form_for([@article,@category]) in your form and use the build method in your controller. If you can clarify, I might be able to help you further (in other words, it's not hard to construct those routes -- but it depends on what you want to do with them.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜