本文介绍了优雅的 Rails:多条路线,相同的控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让多条路线走向相同的最优雅的方式是什么?控制器动作?
What is the most elegant way to have multiple routes go to the samecontroller action?
我有:
get 'dashboard', to: 'dashboard#index'
get 'dashboard/pending', to: 'dashboard#index'
get 'dashboard/live', to: 'dashboard#index'
get 'dashboard/sold', to: 'dashboard#index'
这太丑了.有什么更优雅"的推荐吗?
一个班轮的奖励积分.
This is quite ugly. Any "more elegant" recommendations?
Bonus points for a one liner.
推荐答案
为什么不只有一个路由和一个控制器操作,并根据传递给它的参数来区分功能?
Why not have just one route and one controller action, and differ the functionality based on params passed to it?
get 'dashboard', to: 'dashboard#index'
app/controller/dashboard_controller.rb
def index
...
if params[:pending]
# pending related stuff
end
if params[:live]
# live related stuff
end
if params[:sold]
# sold related stuff
end
...
end
视图中的链接
- pending:
<%= link_to "Pending", dashboard_path(pending: true) %>
- live:
- sold:
这篇关于优雅的 Rails:多条路线,相同的控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!