Rails路由到资源的特定操作

时间:2016-07-28 08:53:53

标签: ruby-on-rails routes

我正在尝试获取给定参数的网址:

url_for({
  :action => "show",
  :controller => "questions",
  :dashboard_id => "123",
  :dashboard_type => "mono",
  :question_id => "1234",
  :only_path => true
})

但是我收到了这个错误:

ActionController::RoutingError: No route matches {:action=>"show", :controller=>"questions", :dashboard_id=>"123", :dashboard_type=>"mono", :question_id=>"1234"}

在我的routes.rb文件中,我有这样的配置:

resources :dashboards, :only => [:index, :all, :create] do
  resources :questions, :path => '/:dashboard_type/questions'
end

什么似乎是问题?

1 个答案:

答案 0 :(得分:2)

:question_id替换为:id

应该是

url_for({
  :action => "show",
  :controller => "questions",
  :dashboard_id => "123",
  :dashboard_type => "mono",
  :id => "1234",
  :only_path => true
})

由于没有:question_id

dashboard_question GET    /dashboards/:dashboard_id/:dashboard_type/questions/:id(.:format)      questions#show