Eri*_* M. 26 routing routes ruby-on-rails
我有一系列资源,只有通过JS格式访问才能使用.Rails的路由资源为我提供了格式和标准HTML.有没有办法指定只创建JS格式的路由?
koo*_*nse 63
您必须将这些路由包装在范围中.遗憾的是,在这种情况下,约束条件无法正常工作.
这是这样一个块的一个例子......
scope :format => true, :constraints => { :format => 'json' } do
get '/bar' => "bar#index_with_json"
end
Run Code Online (Sandbox Code Playgroud)
更多信息可以在这里找到:https://github.com/rails/rails/issues/5548
shi*_*ara 17
您只需添加有关格式的约束:
resources :photos, :constraints => {:format => /(js|json)/}
Run Code Online (Sandbox Code Playgroud)
小智 5
上述解决方案均不适合我.我最终选择了这个解决方案:
post "/test/suggestions", to: "test#suggestions", :constraints => -> (req) { req.xhr? }
Run Code Online (Sandbox Code Playgroud)
在https://railsadventures.wordpress.com/2012/10/07/routing-only-ajax-requests-in-ror/#comment-375上找到
| 归档时间: |
|
| 查看次数: |
15904 次 |
| 最近记录: |