简体   繁体   中英

Rails: Adding routes to singleton resource

So, if I have a singleton route...

resource :shared

... and I want to have custom action names instead of the standard CRUD names, do I still do...

resource :shared do
  collection do
    get 'foo'
    get 'bar'
    match 'baz'
  end
end

... or can I go without the "collection" block and pass those routes directly to the singleton?

In this case I don't want any of the standard CRUD routes either...

Is there a simpler, cleaner way to setup routes to only the specific ajax related actions for my shared resources? I'm using this controller very sparsely, mostly it exists so that the shared view folder can serve things like my footer and my menus...

Suggestions?

You could use match

Say for example you have a controller called FooController and a method called bar you could do the following:

match "/foo/bar/" => "foo#bar", :as => "foo_bar"

That would match any request, be it GET or POST, to your FooController with method bar

Replace match with post or get if you only want one specific http verb.

The resource code looks like this:

collection do
  post :create
end if parent_resource.actions.include?(:create)

new do
  get :new
end if parent_resource.actions.include?(:new)

member do
  get    :edit if parent_resource.actions.include?(:edit)
  get    :show if parent_resource.actions.include?(:show)
  put    :update if parent_resource.actions.include?(:update)
  delete :destroy if parent_resource.actions.include?(:destroy)
end

So you can basically do the same thing, but with your own names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM