开发者

Can you render a single instance of an object through a partial using collections?

开发者 https://www.devze.com 2023-04-08 23:16 出处:网络
So for example, if I have a partial that works with @users as a collection, i.e. <%= render :partial => \'dashboard/dashboard_pane\', :collection => @users %>

So for example, if I have a partial that works with @users as a collection, i.e.

<%= render :partial => 'dashboard/dashboard_pane', :collection => @users %>

where

@users = User.all

But DOESNT seem to work with a single instance

<%= render :partial => 'dashboard/dashboard_pane', :collection => @user %>

where

开发者_如何学JAVA
@user = User.first

The questions is, why?


I guess it's looping an Array.

Try with:

[ User.first ]


To use this partial with a single object, you should't be calling it with :collection. Try this instead:

<%= render :partial => 'dashboard/dashboard_pane', :object => @users.first %>


I think the answer is because it's not a collection :)

As @apneadiving said try putting it in an array. A very contrived alternative is to make it into a bona fide collection:

@users = User.find_all_by_id(@user.id)

I wouldn't recommend this though.

0

精彩评论

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