0

我想访问我为特定路线定义的默认值。但 Rails 拒绝将其放入params哈希中。例子:

路线.rb:

get "packages(/:display)", to: "packages#index", defaults: { display: 'grid' }

我调用的网址:

http://localhost:3000/packages

参数哈希变为:

{"action"=>"index", "controller"=>"packages"}

但我更希望得到:

{"action"=>"index", "controller"=>"packages", "display"=>"grid"}

问题真的不大。但是 Rails 声称 DRY 如此,所以我认为我可以在routes.rb中保留默认值,而不是在我的控制器或查看代码中重复它们。

我的意图是显示结果网格或结果列表。如果用户没有指定某个视图,我想使用网格样式。

(我正在使用 Rails 4。)

4

1 回答 1

0

我刚刚在我的 Rails 4 应用程序中测试了以下路线。

get "packages(/:display)", to: "packages#index", defaults: { display: "grid" }
  • /packages:params[:display]"grid"
  • /packages/list:params[:display]"list"

It's likely you have other routes playing into this still, preventing the same results I lay out above.

Short of finding such conflicts, how about we avoid the optional segment (the result is the same as above)?

get "packages",          to: "packages#index", display: "grid"
get "packages/:display", to: "packages#index"
于 2013-09-05T00:32:41.853 回答