Mateu
Mateu

Reputation: 2698

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

Upvotes: 6

Views: 1011

Answers (1)

Daniel Rikowski
Daniel Rikowski

Reputation: 72504

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.

Upvotes: 8

Related Questions