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

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

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.

Related

How to use single html.erb file for multiple actions

I want to create multiple function but each functions render to its data to single file as like simple php, is possible with ruby and rails. I tried to found this but search results are not as per my point of view.
Partials will help you:
Create view than you want show in other views, name it _your_view.html.erb
Include it in other views:
<%= render :partial => 'path/to/your/partial/your_view' %>
Path can be skipped if your partial is in /app/views root folder, not in subfolders.
Don't forget remove first _ symbol from partial name while including (file is _your_view.html.erb and including is your_view).
Also you can pass parameters into partial:
<%= render :partial => 'path/to/your/partial/your_view', :locals => {:param1 => 'value1' } %>
and use them in partial:
<% value = local_assigns[:param1] %>
<%= text_field_tag :param1, :value => value %>
Yes, this is possible. You should look at using custom View helpers. For start, look at these links:
http://www.rails-dev.com/custom-view-helpers-in-rails-4
http://api.rubyonrails.org/classes/ActionView/Helpers.html

how to use partial

I'm new to Ruby on Rails. I have to show the names of interviewers from the table interviews in a partial view of interviewers. I am calling the scheduled_interviews as partial. How to do it? Am I doing this correctly?
Here's my code:
<%= render :partial=>"scheduled_interviews", :locals=>{
:interviews=>Interview.find(:interviewer_name=>#interviewers.name)
}%>
If the partial name is _scheduled_interviews.htm.erb for example then calling the partial will be <%= render :partial => "scheduled_interviews" %>. If you want to pass some parameters to the partial then use the locals attribute as you mentioned in the question render :partial => "scheduled_interviews", :locals=> {:interviews => Interview.find_by_interviewer_name(#interviewer.name)} to pass a collection (for example an array you could use the :collection parameter. I highly recommend you read the following tutorial

Rails partial collection empty?

I've been trying to figure this one out for about an hour now. I have a partial collection that is indeed populated with member model objects, and when I try to render it using collection:
<%= render :partial => 'list', :collection => #members %>
In my _list.html.erb the #member_id and #member_email attributes are empty in the rendered html:
<%= check_box_tag('selected_member[]', #member_id, false) %>
<%= #member_email %>
The view renders correctly and I get 3 member items, but with the id and email blank.
What am I doing wrong? If I try to access the partial member using "member_id" or "member.id" I get "undefined local variable or method `member'", so clearly that's not the correct notation.
Now I've seen a bunch of different examples, but the collection method is the way I need to go. I need the partial to only deal with each member as the parent view has support for grouping members in different ways. If I implement a loop in the partial for each member, I'll have to move the grouping view code into it as well, which interacts with input fields in the parent view. e.g. it would be a mess to maintain.
Thanks in advance.
When you do:
<%= render :partial => 'list', :collection => #members %>
Rails is expecting the local variable within the partial file to be named the same as the partial file itself, in this case list.
So instead of member, by default it would be list like this:
<%= check_box_tag('selected_member[]', list.id, false) %>
<%= list.email %>
If you want the local variable to instead be named member, you should either rename the partial file or use this syntax:
<%= render :partial => 'list', :collection => #members, :as => :member %>
More info here (Sec. 3.4.5): http://guides.rubyonrails.org/layouts_and_rendering.html

Using scope with a view partial

I have been using a scope to present some information to show todos that have been completed and are 24 hours old
scope :completed, joins(:user).where(:todos => { :complete => true })
scope :logged, completed.where("todos.updated_at <= ?", 1.day.ago)
Using the regular todo partial
<%= render #user.todos.logged =>
However I want to present those logged items in a different partial _logged.html.erb. I just can't figure out the proper way to pass scope results to a specific partial.
Thanks
Well, if you want to render partial for each item, yo can do:
<%=render :partial=> 'logged', :collection=>#user.todos.logged %>
Or if you want to pass the whole array to one instance then you can do
<%=render :partial=> 'logged', :object=>#user.todos.logged %>
In both cases I guess your object will be called logged.
Assuming that your partial contains <%= logged.title %> you want to render for each item, so you can use the first version.
First, to keep my conscience clean, let me say that that passing model code to your views is never a good idea. But if you insist :
<%= render :partial => 'some_partial',
:locals => {:some_variable => "somevalue",
:some_other_variable => some_other_variable} %>

Accessing nested model attributes inside a fields_for without using FormBuilder

I have a Rails form that is being used for creating and editing a model with a has_many relationship. I'm very familiar with typical forms with nested models, but my current problem requires accessing rendering some form elements using typical FormBuilder helpers and other HTML elements using data from the model itself. For example, my top level form has something like:
<% customer_form.fields_for :customer_images do |images_form| %>
<%= render :partial => 'customer_image_show', :locals => { :f => images_form } %>
<% end %>
Then, in the form partial, I need to do something like:
<dd><%= f.text_field :image_description %></dd>
... but also access attributes from the customer_images model (for example, the ID of the customer_image record).
I feel like this should be pretty straightforward and I'm just missing something basic. Any help is appreciated. This is a Rails 2.3.8 application.
You can call
f.object
to get to the object that the form is associated with.
Not sure if this is what you need, but couldn't you include the model data as a local? like:
<%= render :partial => 'customer_image_show', :locals => { :f => images_form, :customer_image => #customer_image } %>

Resources