Tôi đang gặp rắc rối định tuyến là một hình thức để một hành động tùy chỉnh trong Rails 3. Dưới đây là tuyến đường của tôi:Rails 3 thức biểu diễn cho Tuỳ chỉnh hành động
resources :photos do
resources :comments
collection do
get 'update_states'
end
member do
put 'upload'
end
end
Đây là form_for:
form_for @photo, :remote => true, :url => { :action => upload_photo_path(@photo) }, :html => { :multipart => :true, :method => 'put' } do |f|
Và đây là thông báo lỗi:
No route matches {:action=>"/photos/42/upload", :controller=>"photos"}
... điều này đặc biệt bực bội vì "ảnh /: id/tải lên" là chính xác hành động chính xác cho biểu mẫu này.
Tôi đang thiếu gì?
CHỈNH SỬA - Dưới đây là những hình ảnh liên quan đến gốc các tuyến đường:
photo_comments GET /photos/:photo_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /photos/:photo_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_photo_comment GET /photos/:photo_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_photo_comment GET /photos/:photo_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
photo_comment GET /photos/:photo_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /photos/:photo_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /photos/:photo_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
update_states_photos GET /photos/update_states(.:format) {:action=>"update_states", :controller=>"photos"}
upload_photo PUT /photos/:id/upload(.:format) {:action=>"upload", :controller=>"photos"}
photos GET /photos(.:format) {:action=>"index", :controller=>"photos"}
POST /photos(.:format) {:action=>"create", :controller=>"photos"}
new_photo GET /photos/new(.:format) {:action=>"new", :controller=>"photos"}
edit_photo GET /photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"}
photo GET /photos/:id(.:format) {:action=>"show", :controller=>"photos"}
PUT /photos/:id(.:format) {:action=>"update", :controller=>"photos"}
DELETE /photos/:id(.:format) {:action=>"destroy", :controller=>"photos"}
Dưới đây là các tuyến đường có liên quan khi tôi đã thay đổi con đường để match 'upload'
:
photo_comments GET /photos/:photo_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /photos/:photo_id/comments(.:format) {:action=>"create", :controller=>"comments"}
}
new_photo_comment GET /photos/:photo_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_photo_comment GET /photos/:photo_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
photo_comment GET /photos/:photo_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /photos/:photo_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /photos/:photo_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
update_states_photos GET /photos/update_states(.:format) {:action=>"update_states", :controller=>"photos"}
upload_photo /photos/:id/upload(.:format) {:action=>"upload", :controller=>"photos"}
photos GET /photos(.:format) {:action=>"index", :controller=>"photos"}
POST /photos(.:format) {:action=>"create", :controller=>"photos"}
new_photo GET /photos/new(.:format) {:action=>"new", :controller=>"photos"}
edit_photo GET /photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"}
photo GET /photos/:id(.:format) {:action=>"show", :controller=>"photos"}
PUT /photos/:id(.:format) {:action=>"update", :controller=>"photos"}
DELETE /photos/:id(.:format) {:action=>"destroy", :controller=>"photos"}
Thật không may 'trận đấu' không làm việc tốt hơn ...
- EDIT -
Chỉ cần để xác nhận kịch bản khác đây ... với điều này trong các tuyến đường:
resources :photos do
resources :comments
collection do
get 'update_states'
end
member do
match 'upload'
end
end
và điều này trong giao diện:
form_for @photo, :remote => true, :url => { :action => 'upload' }, :html => { :multipart => :true, :id => 'photo_upload' } do |f|
tôi vẫn nhận được:
No route matches {:action=>"upload", :controller=>"photos"}
Tôi có thể thử điều đó sau, nhưng tôi đã quyết định đi theo một hướng hoàn toàn mới. Lý do tải lên thành viên là tôi đã tạo, chuẩn bị và tiết kiệm khi người dùng nhấp vào 'mới', sau đó họ thực sự chỉnh sửa bản ghi khi họ điền thông tin lần đầu tiên. Tôi đã làm điều này bởi vì tôi muốn để có thể lưu tiến bộ của họ khi họ đi, và có một số mô hình liên quan cần id mẹ để tiết kiệm đúng .... anyway, điều này là quá nhiều đau, vì vậy tôi đang cố gắng để đưa ra một cách tiếp cận hoàn toàn mới. – Andrew
Cũng chỉ để bạn biết, nếu chỉ có bạn đã không sử dụng '{: action => ...}' bit - và thay vào đó đã làm những gì tôi đề nghị ở trên - bạn sẽ thành công. Những gì bạn đã nói với trình tạo trợ giúp tạo url là, trên thực tế, bạn muốn đăng lên một hành động trên bộ điều khiển của bạn, được đặt tên là đầu ra của 'upload_photo_path (@photo)' - mà ** chính nó ** chứa bộ điều khiển và tên hành động (như bạn có thể thấy trong lỗi "No route matches ...") – davemyron
Ok, tôi đã kiểm tra điều này để chắc chắn, và bạn nói đúng, điều này đã tạo ra con đường như bạn đã nói. Tôi sẽ không sử dụng nó theo cách này nữa (các vấn đề khác với việc thực hiện của tôi), nhưng đây là một giải pháp hợp lệ. Cảm ơn :) – Andrew