How to add a new custom resource to routes Rails 3

僤鯓⒐⒋嵵緔 提交于 2019-12-23 03:15:47

问题


How do I add a custom route for a new resource in the Rails 3 routes?

I know how to do it for collections and members but this style doesn't seem to be working for new resources. Is this a bug or am I doing something wrong?

So these work:

collection do
  get :wish
end

member do
  get :wish
end

But this doesn't work:

new do
  get :wish
end

回答1:


Try this:

resources :<resource name> do
  member do
    get '<custom action>'
  end
end

As an example lets see you have a controller called 'main' and if you have a custom action 'dashbord'

resources :admin do
  member do
    get 'dashbord'
  end
end



回答2:


In other words you want to match to something like:

example.com/foos/new/custom rather than example.com/foos/1/custom/ or example.com/foos/custom

That's not RESTful, which just means there isn't an automatic route for it. You should be able to do it using non-resourceful routing, ie something like this should work:

match 'resource/new/custom'=>'resource#custom'

... where 'custom' is the action name in your controller.

See the rails guide for more options and details.



来源:https://stackoverflow.com/questions/2920855/how-to-add-a-new-custom-resource-to-routes-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!