Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

ruby on rails - Passing parameters to partial view

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

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<%= render partial: "image_tag", locals: {image: i} %>

is how you would pass variables to partials.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...