我有一个带有“患者”的 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 变形器上添加 make “default” 和 “pilates” 不可数;否则我得到一些名为“defaults”的路线和其他名为“pilate”的路线:
#config/initialize/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( default pilates )
end
我的问题是我在“显示”操作中解决的任何“新”操作链接。例如,这个:
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,没有“批量分配问题”,也不是权限问题。控制器没有收到“新”动作,然后检测到错误并重定向到“显示”或类似的东西。
确实似乎该 url 被错误地解析了。
我已经为此投入了几个小时。如果有人对如何解决它有任何指示,我将不胜感激。