我有一個Rails 3.2.2應用與「患者」誰可以有幾種類型的「約會」的。所以,我提出以下途徑:軌道3嵌套/命名空間的路線產生秀行動,而不是新的
# 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
注意,我不得不添加使「默認」和「普拉提」不可數的軌道偏轉;否則我得到了一些名爲「默認」的路線和其他名爲「彼拉多」:
#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, ...>})
我已經檢查了明顯的事情; @病人對象不是零,沒有「大規模分配問題」,這不是一個權限問題。控制器沒有收到「新」動作,然後檢測到錯誤並重定向到「顯示」或類似的東西。
這真的似乎URL被嚴重解析。
我已經在此投入了幾個小時。如果任何人有任何關於如何解決它的指示,我會非常感激。
你的'DefaultController#new'動作是什麼? Rails明確地將URL映射到正在加載用戶和病人的「新」操作。但之後的某個地方它爆炸了。 –
@RobDavis感謝您的評論。我想我只是找到了它。請參閱下面的答案。 – kikito
在您的控制器或視圖中可能是不好的路徑。 – Stefan