简体   繁体   English

Rails:向单例资源添加路由

[英]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... ...我希望有自定义动作名称而不是标准的CRUD名称,我还能做...

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... 在这种情况下,我不想要任何标准的CRUD路线......

Is there a simpler, cleaner way to setup routes to only the specific ajax related actions for my shared resources? 有没有更简单,更简洁的方法来设置路由到我的共享资源的特定ajax相关操作? 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... 我非常稀疏地使用这个控制器,大多数它存在,以便shared视图文件夹可以提供像我的页脚和我的菜单...

Suggestions? 建议?

You could use match 你可以使用match

Say for example you have a controller called FooController and a method called bar you could do the following: 比如说你有一个名为FooController的控制器和一个名为bar的方法你可以执行以下操作:

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

That would match any request, be it GET or POST, to your FooController with method bar 这将匹配任何请求,无论是GET还是POST,与方法bar FooController

Replace match with post or get if you only want one specific http verb. 如果您只需要一个特定的http动词,请将match替换为postget

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. 所以你基本上可以做同样的事情,但用你自己的名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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