如何在rails 3.1中为作用域/嵌套资源创建named_route_path?
这是我的路线:
scope ":username" do resources :feedbacks end
当我做rake routes
,我明白了:
feedbacks GET /:username/feedbacks(.:format) {:action=>"index", :controller=>"feedbacks"} POST /:username/feedbacks(.:format) {:action=>"create", :controller=>"feedbacks"} new_feedback GET /:username/feedbacks/new(.:format) {:action=>"new", :controller=>"feedbacks"} edit_feedback GET /:username/feedbacks/:id/edit(.:format) {:action=>"edit", :controller=>"feedbacks"} feedback GET /:username/feedbacks/:id(.:format) {:action=>"show", :controller=>"feedbacks"} PUT /:username/feedbacks/:id(.:format) {:action=>"update", :controller=>"feedbacks"} DELETE /:username/feedbacks/:id(.:format) {:action=>"destroy", :controller=>"feedbacks"}
但是当我做feedbacks_url或feedbacks_path时,我得到一个像这样的路由错误:
No route matches {:controller=>"feedbacks", :format=>nil}
从日志文件:
Rendered users/show.html.erb within layouts/application (18.3ms) Completed 500 Internal Server Error in 142ms ActionView::Template::Error (No route matches {:controller=>"feedbacks", :format=>nil}): 1: 2: 3: 4: prohibited this feedback from being saved:
app/views/feedbacks/_form.html.erb:1:in `_app_views_feedbacks__form_html_erb__2195884682603870163_2484875900' app/views/users/show.html.erb:11:in `_app_views_users_show_html_erb__3889159515317937411_2159438040' app/controllers/vanities_controller.rb:14:in `show'
这是部分feedbacks/_form.html.erb
的forms:
prohibited this feedback from being saved:
current_user.id %>
@user.id %>
思考?
好,
scope ":username" do
意味着您的路线需要以下信息:username
才能正常工作。
然后,您应该在所有链接中添加此信息:
user_path(user, :username => "joe") edit_user_path(user, :username => "joe") ...
或者您可以告诉您的应用程序username
不是强制性的,如果用户尚未创建,这似乎是逻辑。 所以改变您的路线:
scope "(:username)" do resources :feedbacks end