Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails: render a collection of models using an specific html view

I have the following simple problem with rails.

Let say I have a model User. In a view, if I do:

<%= render User.all %>

The file view in views/user/_user.html.erb will be called and printed for each of the users.

How can I change this to use an specific view? I need something like:

<%= render :data=>User.all :template=>"user/_user_2ndview.html"%>

Any help? Thanks in advance

like image 303
Mateu Avatar asked Dec 19 '12 18:12

Mateu


1 Answers

You can use the collection option:

<%= render :collection => User.all, :partial => "users/user2ndview", 
           :as => :user %>

The view must be placed in views/users/_user2ndview

See the Rails guides on rendering collections for more details.

like image 110
Daniel Rikowski Avatar answered Nov 13 '22 08:11

Daniel Rikowski