如何为嵌套资源视图呈现局部视图? -Rails 3.1 [英] How do I render a partial for a view of a nested resource? - Rails 3.1

查看:56
本文介绍了如何为嵌套资源视图呈现局部视图? -Rails 3.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的路线:

scope ":username" do
  resources :feedbacks
end

我在我的 home / index.html.erb 视图,而我正在尝试这样做:

I am on my home/index.html.erb view and I am trying to do this:

<%= 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控制器中提取的,它的声明如下:

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:

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

end

我仍然收到此错误:

Routing Error

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

编辑2

我也有个用户在我的路线文件中指定的资源,如下所示:

I also have a users resource specified in my routes file, that looks like this:

 resources :users

    scope ":username" do
      resources :feedbacks
    end


推荐答案

似乎像 form_for 找不到正确的路线,此行表明您没有实例变量 @feedback

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:

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

请注意,没有:id 参数(可能会传递给路由器以生成正确的URL来提交表单)。

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).

您是否定义了 @feedback 在控制器操作中的某个位置(首选)或您正在使用的视图?如果您认为自己是,请尝试下面的部分代码的第1行:

Do you define a @feedback somewhere in the controller action (preferable) or views you're using? If you think you are, try the following above line 1 of your partial:

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

编辑

首先,不应该将本地人作为:local =>传递。 @feedback 但为:

First of all, locals should be passed not as :local => @feedback but as:

:locals => {:variable_name_in_partial => value_for_variable_name}

第二,您尝试访问 @feedback 是实例视图变量,即使未显式传递(只要已设置),视图也可以访问。因此,只要在 HomeController#index 方法中进行设置,就无需将其传递给本地任何视图。

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). So as long as you set it in the HomeController#index method you don't need to pass it to any views locally.

此外,您没有使用正确的语法来渲染局部图像(这是我之前注意到的!)。

Also, you are not using the correct syntax for rendering partials (wish I had noticed earlier!).

更改以下内容:

<%= render "feedbacks/form" %>

到此:

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

这篇关于如何为嵌套资源视图呈现局部视图? -Rails 3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆