这是我的路线:
scope ":username" do
resources :feedbacks
end
我正在查看home/index.html.erb
视图,我正在尝试这样做:
<%= render "feedbacks/form" %>
但这给了我这个错误:
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__3181571289116259961_2487495480'
app/views/home/index.html.erb:18:in `_app_views_home_index_html_erb___397233383548615486_2487321620'
这是我的反馈路线:
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"}
修改1
当我传入这样的本地实例变量@feedback
时:<%= render "feedbacks/form", :local => @feedback %>
从我的home controller
拉出来,其中声明如下:
class HomeController < ApplicationController
def index
@users = User.all
@feedback = Feedback.new
end
end
我仍然收到此错误:
Routing Error
No route matches {:controller=>"feedbacks", :format=>nil}
修改2
我的路由文件中也指定了users
资源,如下所示:
resources :users
scope ":username" do
resources :feedbacks
end
答案 0 :(得分:2)
似乎form_for
找不到正确的路由,这一行表示在尝试渲染部分时没有设置实例变量@feedback
:
(No route matches {:controller=>"feedbacks", :format=>nil})
请注意,该哈希中没有:id
参数(可能会传递给路由器以生成提交表单的正确URL)。
您是在控制器操作中的某个位置(首选)或您正在使用的视图中定义@feedback
吗?如果您认为自己是,请尝试以下第1行的部分内容:
logger.info "feedback object: #{@feedback.inspect}"
修改强>:
首先,本地人不应该作为:local => @feedback
而是作为:
:locals => {:variable_name_in_partial => value_for_variable_name}
其次,您正在尝试访问@feedback
,这是一个视图可访问的实例变量,即使它未被显式传递(只要它已设置)。因此,只要您在HomeController#index
方法中设置它,就不需要将其传递给本地任何视图。
此外,您没有使用正确的语法来渲染部分(希望我之前已经注意到了!)。
更改以下内容:
<%= render "feedbacks/form" %>
对此:
<%= render :partial => "feedbacks/form" %>
答案 1 :(得分:1)
你不忘记传递:username
吗?您的路线显示scope :username
如果我添加像您这样的路由,打开Rails控制台include Rails.application.routes.url_helpers
并运行feedbacks_path
,我收到路由错误,但feedbacks_path :username => 'bob'
工作正常。
> feedbacks_path(:username => 'bob')
=> "/bob/feedbacks"
答案 2 :(得分:0)
你确定你想要范围吗?那么嵌套资源如何:
resources :users do
resources :feedbacks
end
另外,我在使用多个名字的控制器时遇到了麻烦。您可以尝试将FeedbacksController
重命名为FeedbackController
。