- 首页
- 其他开发
- 如何在 rails 3.1 中为作用域/嵌套资源创建 named_route_path?
如何在 rails 3.1 中为作用域/嵌套资源创建 named_route_path?
[英] How do I create a named_route_path for a scoped/nested resource in rails 3.1?
本文介绍了如何在 rails 3.1 中为作用域/嵌套资源创建 named_route_path?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的路线:
scope ":username" 做资源:反馈结尾
当我做rake routes
时,我得到了这个:
反馈 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"}反馈 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 时,我收到了这样的路由错误:
没有路由匹配 {:controller=>"feedbacks", :format=>nil}
来自日志文件:
在布局/应用程序中渲染 users/show.html.erb (18.3ms)在 142 毫秒内完成 500 个内部服务器错误ActionView::Template::Error(没有路由匹配 {:controller=>"feedbacks", :format=>nil}):1: <%= form_for(@feedback) 做 |f|%>2: <% if @feedback.errors.any?%>3:<div id="error_explanation">4:<h2><%=复数(@feedback.errors.count, "error") %>禁止保存此反馈:</h2>app/views/feedbacks/_form.html.erb:1:在`_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
:
<%= form_for(@feedback) do |f|%><% if @feedback.errors.any?%><div id="error_explanation"><h2><%=pluralize(@feedback.errors.count, "error") %>禁止保存此反馈:</h2><ul><% @feedback.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%><% if current_user %><div class="poster_id"><%= f.hidden_field "poster_id", :value =>current_user.id %><br/>
<div class="receiver_id"><%= f.hidden_field "receiver_id", :value =>@user.id %><br/>
<div class="field"><%= f.text_field :content %>
<div class="actions"><%= f.submit %>
<%结束%><%结束%>
想法?
解决方案
嗯,
scope ":username" 做
意味着你的路由需要一些关于 :username
的信息才能正常工作.
然后您应该在所有链接中添加此信息:
user_path(user, :username => "joe")edit_user_path(user, :username => "joe")...
或者您可以告诉您的应用 username
不是强制性的,如果尚未创建用户,这似乎是逻辑.因此,将您的路线更改为:
scope "(:username)" 做资源:反馈结尾
This is my route:
scope ":username" do
resources :feedbacks
end
When I do rake routes
, I get this:
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"}
But when I do feedbacks_url or feedbacks_path I get an routing error like this:
No route matches {:controller=>"feedbacks", :format=>nil}
From the log file:
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: <%= form_for(@feedback) do |f| %>
2: <% if @feedback.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@feedback.errors.count, "error") %> prohibited this feedback from being saved:</h2>
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'
This is the form partial feedbacks/_form.html.erb
:
<%= form_for(@feedback) do |f| %>
<% if @feedback.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@feedback.errors.count, "error") %> prohibited this feedback from being saved:</h2>
<ul>
<% @feedback.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if current_user %>
<div class="poster_id">
<%= f.hidden_field "poster_id", :value => current_user.id %><br />
</div>
<div class="receiver_id">
<%= f.hidden_field "receiver_id", :value => @user.id %><br />
</div>
<div class="field">
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% end %>
Thoughts?
解决方案
Well,
scope ":username" do
means that your routes need some info about :username
to work properly.
You should then add this info in all your links:
user_path(user, :username => "joe")
edit_user_path(user, :username => "joe")
...
Or you could tell your app the username
isn't mandatory which seems logic if user isn't created yet. So change your route to:
scope "(:username)" do
resources :feedbacks
end
这篇关于如何在 rails 3.1 中为作用域/嵌套资源创建 named_route_path?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!