问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/7843370/how-do-i-create-a-named-route-path-for-a-scoped-nested-resource-in-rails-3-1