rails 3嵌套/命名空间路由生成show动作而不是new

时间:2012-06-18 09:39:04

标签: ruby ruby-on-rails-3 routing

我有一个带有“患者”的rails 3.2.2应用程序,可以有几种类型的“约会”。所以我做了以下路线:

# config/routes.rb
resources :patients do
  namespace :appointments do
    resources :default, :except => [:index]                                           
    resources :pilates, :except => [:index]
  end
end

相关rake routes输出:

patient_appointments_default_index POST   /patients/:patient_id/appointments/default(.:format)          appointments/default#create
  new_patient_appointments_default GET    /patients/:patient_id/appointments/default/new(.:format)      appointments/default#new
 edit_patient_appointments_default GET    /patients/:patient_id/appointments/default/:id/edit(.:format) appointments/default#edit
      patient_appointments_default GET    /patients/:patient_id/appointments/default/:id(.:format)      appointments/default#show
                                   PUT    /patients/:patient_id/appointments/default/:id(.:format)      appointments/default#update
                                   DELETE /patients/:patient_id/appointments/default/:id(.:format)      appointments/default#destroy
patient_appointments_pilates_index POST   /patients/:patient_id/appointments/pilates(.:format)          appointments/pilates#create
  new_patient_appointments_pilates GET    /patients/:patient_id/appointments/pilates/new(.:format)      appointments/pilates#new
 edit_patient_appointments_pilates GET    /patients/:patient_id/appointments/pilates/:id/edit(.:format) appointments/pilates#edit
      patient_appointments_pilates GET    /patients/:patient_id/appointments/pilates/:id(.:format)      appointments/pilates#show
                                   PUT    /patients/:patient_id/appointments/pilates/:id(.:format)      appointments/pilates#update
                                   DELETE /patients/:patient_id/appointments/pilates/:id(.:format)      appointments/pilates#destroy

请注意,我必须在rails inflector上添加make“default”和“pilates”不可数;否则我得到一些名为“默认”的路线和其他名为“pilate”的路线:

#config/initialize/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w( default pilates )
end

我的问题是我在“show”操作中解决的任何“新”操作链接。例如,这个:

link_to "Add", new_patient_appointments_default_path(@patient)

生成此(有效的,恕我直言)网址:

/patients/14/appointments/default/new

但是当我点击它时,我收到了这个错误:

Started GET "/patients/14/appointments/default/new" for 127.0.0.1 at 2012-06-18 11:29:31 +0200
Processing by Appointments::DefaultController#new as HTML
  Parameters: {"patient_id"=>"14"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Patient Load (0.2ms)  SELECT "patients".* FROM "patients" WHERE "patients"."id" = ? LIMIT 1  [["id", "14"]]
  ...
Completed 500 Internal Server Error in 512ms

ActionController::RoutingError (No route matches {
  :action => "show",
  :controller => "appointments/default",
  :patient_id => #<Patient id: 14, ...>,
  :id => #<Appointments::Default id: nil, ...>})

我检查过明显的事情; @patient对象不是nil,没有“批量分配问题”,并且它不是权限问题。控制器没有收到“新”操作,然后检测到错误并重定向到“显示”或类似的东西。

网址似乎很难被解析。

我已经投入了几个小时。如果有人对如何修复它有任何指示,我将非常感激。

1 个答案:

答案 0 :(得分:0)

道歉。

我尝试为“新”动作渲染一个简单的“仅文本”视图,并且它有效。所以现在我认为这是“新”视图中的一个错误。我仍然不知道为什么Rails试图渲染“show”视图而不是告诉我有关错误的信息。但我想我可以在这里工作。一旦SO让我,就会关闭这个答案。