将参数传递给局部视图 [英] Passing parameters to partial view

查看:45
本文介绍了将参数传递给局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示多个图像和这些图像的关联标签的视图.我决定为每个图像及其标签使用局部视图,但是我无法将图像对象传递到局部视图中.这是主视图的相关代码:

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>

这里是局部视图的相关代码(局部视图被命名为_image_tag.html.erb):

Here is the partial view's relevant code (partial view is named _image_tag.html.erb):

<table>
  <%= image.id %>
  <%= image_tag image.src %>
</table>

我阅读了这个线程,我可以通过在图像对象中,我目前正在这样做.我尝试通过渲染方法上的选项哈希传递 id,但这也不起作用.我得到的错误是:

I read in this thread 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:

undefined method `model_name' for Symbol:Class

以我在主视图中调用 render :partial 的行为中心.

centered around the line where I am calling render :partial in the main view.

推荐答案

<%= render partial: "image_tag", locals: {image: i} %>

是将变量传递给部分的方式.

is how you would pass variables to partials.

这篇关于将参数传递给局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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