开发者

Trouble on loading multiple partial templates in a view

I am using Ruby on Rails 3.0.7 and I would like to understand how can I handle the following situation to avoid the istance variable @photo overriding on loading the view.

In my view file I have:

<div>
  <%=
    render :partial => 'user/photos',
           :locals  => {
             :photo => @photo = 'test_photo1.jpg',
           }
  %>
</div>

<div>
  <%=
    render :partial => 'user/photos',
           :locals  => {
             :photo => @photo = 'test_photo2.jpg',
           }
  %>
</div>

If I load the above view I have a strange behavior on outputting. That seams that the @photo class is overwritten (by the second rendering statement) when the page loads. Of course if I make only one rendering all works.

How can I solve the above problem in order to properly pass variables?

Note: I can not change the @photo name. That is, it must be the same for both partial templates.


I also tryed this version just deleting the @photo variable

<div>
  <%=
    render :partial => 'user/photos',
           :locals  => {
             :photo => 'test_photo1.jpg',
           }
  %>
</div>

<div>
  <%=
    render :partial => 'user/photos',
           :locals  => {
             :photo =&g开发者_如何学Got; 'test_photo2.jpg',
           }
  %>
</div>

but that still doesn't work.


<div>
  <%=
    render :partial => 'user/photos',
           :locals  => {:photo = 'test_photo2.jpg'}
  %>
</div>

Try that, I dont know why you had :photo => @photo = 'test_photo2.jpg'

Also see the Rails guides on partials. They really let you have a good grasp of the basics.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜