拥有多条路线前往同一条路线的最优雅方法是什么
Controller Action ?

我有:

  get 'dashboard', to: 'dashboard#index'
  get 'dashboard/pending', to: 'dashboard#index'
  get 'dashboard/live', to: 'dashboard#index'
  get 'dashboard/sold', to: 'dashboard#index'

这是非常丑陋的。有“更优雅”的建议吗?
一支类轮的奖励积分。

最佳答案

为什么不只有一个路由和一个 Controller Action ,并根据传递给它的参数来区分功能?

config/routes.rb:

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

View 中的链接
  • 待审核:<%= link_to "Pending", dashboard_path(pending: true) %>
  • live:<%= link_to "Live", dashboard_path(live: true) %>
  • 已售出:<%= link_to "Sold", dashboard_path(sold: true) %>
  • 关于ruby-on-rails - 优雅的Rails : multiple routes,相同的 Controller Action ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29174836/

    10-12 22:02