这是我的路线:
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: <%= 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'
这是部分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 %>
思想?
答案 0 :(得分:1)
那么,
scope ":username" do
表示您的路线需要有关:username
的一些信息才能正常使用。
然后,您应该在所有链接中添加此信息:
user_path(user, :username => "joe")
edit_user_path(user, :username => "joe")
...
或者您可以告诉您的应用username
不是必需的,如果尚未创建用户,这似乎是逻辑。所以改变您的路线:
scope "(:username)" do
resources :feedbacks
end