Ruby-on-rails 将参数传递给局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6672454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameters to partial view
提问by jnevelson
I have a view that displays multiple images and those images' associated tags. I decided to use a partial view for each image and its tags, but I am having trouble passing in the image object into the partial view. Here is the main view's relevant code:
我有一个显示多个图像和这些图像的关联标签的视图。我决定为每个图像及其标签使用局部视图,但是我无法将图像对象传递到局部视图中。这是主视图的相关代码:
<table>
<% @images.each do |i| %>
<tr>
<%= render :partial => :image_tag, :image => i %>
</tr>
<% end %>
</table>
Here is the partial view's relevant code (partial view is named _image_tag.html.erb):
下面是局部视图的相关代码(局部视图被命名为 _image_tag.html.erb):
<table>
<%= image.id %>
<%= image_tag image.src %>
</table>
I read in thisthread that I can pass in the image object the way that I am currently doing it. I tried passing in the id through an options hash on the render method, and that also didn't work. The error I am getting is:
我在这个线程中读到我可以按照我目前正在做的方式传入图像对象。我尝试通过渲染方法上的选项哈希传递 id,但这也不起作用。我得到的错误是:
undefined method `model_name' for Symbol:Class
centered around the line where I am calling render :partial in the main view.
以我在主视图中调用 render :partial 的行为中心。
回答by vinceh
<%= render partial: "image_tag", locals: {image: i} %>
is how you would pass variables to partials.
是您将变量传递给部分的方式。
回答by MFarmer
Something else to consider for those that may be having trouble sending value(s) to a partial. If you omit the 'partial:'before your partial path, like so...:
对于那些可能在向部分发送值时遇到问题的人,还需要考虑其他事项。如果您在部分路径之前省略“部分:”,如下所示...:
<%= render 'my_partial', :locals => {:greeting => 'Hello world', :x => 36} %>
...it seems you'll be unable to access the locals hash values directly. Rather, you would need to do the following:
...看来您将无法直接访问本地哈希值。相反,您需要执行以下操作:
<div>
<h1> <%= locals[:greeting] %> , my x value is <%= locals[:x] %> </h1>
</div>
However, including 'partial:'before your partial path, like the following...:
但是,在您的部分路径之前包括“部分:”,如下所示...:
<%= render partial: 'my_partial', :locals => {:greeting => 'Hello world', :x => 36} %>
...allows you to directly access the hash values, like so:
...允许您直接访问哈希值,如下所示:
<div>
<h1> <%= greeting %> , my x value is <%= x %> </h1>
</div>
Just something to consider, I was getting tripped up by this issue when trying to access the locals hash values and realized I had omitted the 'partial:'component.
只是需要考虑的事情,当我试图访问本地哈希值时,我被这个问题绊倒了,并意识到我已经省略了“部分:”组件。
回答by Msencenb
You can also pass the whole object to a partial like this:
您还可以像这样将整个对象传递给部分对象:
<%= render :partial => "partialpath", :object => :image %>
You would replace i with image in your case and partial path with whatever your partial is called. Inside the partial it would have access to a local variable with the same name as the partials name. So if your partials name is "image" the local variable image will be the object you pass in.
在您的情况下,您可以将 i 替换为图像,并将部分路径替换为您的部分名称。在部分内部,它可以访问与部分名称同名的局部变量。因此,如果您的部分名称是“image”,则局部变量 image 将是您传入的对象。
EDIT: Looking at the rails guides looks like in rails 3 the :object is now accessed as an instance variable instead of a local so @image would be what you use in the partial.
编辑:查看 rails 指南看起来像在 rails 3 中 :object 现在作为实例变量而不是本地变量访问,因此 @image 将是您在部分中使用的内容。

