简体   繁体   English

如何为嵌套资源视图呈现局部视图? -Rails 3.1

[英]How do I render a partial for a view of a nested resource? - Rails 3.1

This is my route: 这是我的路线:

scope ":username" do
  resources :feedbacks
end

I am on my home/index.html.erb view and I am trying to do this: 我在home/index.html.erb视图上,正在尝试这样做:

<%= render "feedbacks/form" %>

But this gives me this error: 但这给了我这个错误:

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'

This is my rake routes for feedbacks: 这是我提供反馈意见的耙路:

      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"}

Edit 1 编辑1

When I pass in the local instance variable @feedback like this: <%= render "feedbacks/form", :local => @feedback %> which is pulling from my home controller , where it is declared like this: 当我像这样传递本地实例变量@feedback<%= render "feedbacks/form", :local => @feedback %>从我的home controller提取,声明如下:

class HomeController < ApplicationController
  def index
        @users = User.all
        @feedback = Feedback.new
  end

end

I still get this error: 我仍然收到此错误:

Routing Error

No route matches {:controller=>"feedbacks", :format=>nil}

Edit 2 编辑2

I also have a users resource specified in my routes file, that looks like this: 我的路由文件中还指定了一个users资源,如下所示:

 resources :users

    scope ":username" do
      resources :feedbacks
    end

Seems like form_for can't find the correct route, this line suggests you don't have an instance variable @feedback set when trying to render that partial: 似乎form_for找不到正确的路线,此行建议您在尝试渲染该局部变量时@feedback设置实例变量@feedback

(No route matches {:controller=>"feedbacks", :format=>nil})

Notice there is no :id parameter in that hash (which is likely being passed to the router to generate the correct URL to submit the form to). 请注意,该哈希中没有:id参数(很可能会传递给路由器以生成正确的URL来提交表单)。

Do you define a @feedback somewhere in the controller action (preferable) or views you're using? 您是否在控制器操作的某个位置(最好)或正在使用的视图中定义了@feedback If you think you are, try the following above line 1 of your partial: 如果您认为自己是,请尝试下面的部分代码的第1行:

logger.info "feedback object: #{@feedback.inspect}"

Edit : 编辑

First of all, locals should be passed not as :local => @feedback but as: 首先,不应将本地人传递为:local => @feedback而应传递为:

:locals => {:variable_name_in_partial => value_for_variable_name}

Second of all, you are trying to access @feedback which is an instance variable accessible to views even if it's not passed explicitly (as long as it's set). 第二,您要尝试访问@feedback ,这是视图可访问的实例变量,即使未显式传递(只要已设置)也是如此。 So as long as you set it in the HomeController#index method you don't need to pass it to any views locally. 因此,只要您在HomeController#index方法中进行设置,就无需将其传递给本地的任何视图。

Also, you are not using the correct syntax for rendering partials (wish I had noticed earlier!). 另外,您没有使用正确的语法来渲染局部图像(这是我之前注意到的!)。

Change the following: 更改以下内容:

<%= render "feedbacks/form" %>

To this: 对此:

<%= render :partial => "feedbacks/form" %>

Aren't you forgetting to pass in :username ? 您是否忘了传递:username Your route says scope :username 您的路线显示scope :username

If I add a route like yours, open up the Rails console, include Rails.application.routes.url_helpers and run feedbacks_path , I get a routing error, but feedbacks_path :username => 'bob' works fine. 如果我添加了像您一样的路由,请打开Rails控制台, include Rails.application.routes.url_helpers并运行feedbacks_path ,我会收到路由错误,但是feedbacks_path :username => 'bob'可以正常工作。

> feedbacks_path(:username => 'bob')
=> "/bob/feedbacks"

Are you sure you want scope? 您确定要范围吗? What about a nested resource as per: 嵌套资源如下:

resources :users do
  resources :feedbacks
end

Also, I've had trouble when using controllers with plural names. 另外,在使用带有复数名称的控制器时遇到了麻烦。 You might try renaming FeedbacksController to FeedbackController . 您可以尝试将FeedbacksController重命名为FeedbackController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM