Can you pass extra variables when rendering a collection in Rails? - ruby-on-rails

I've been using this pattern a lot in a recent project:
<%= render partial: 'path/to/partial', collection: #crowd, as: :person %>
What I'm wondering if it's possible to set another local variable using the same syntax (rather than eg falling back on an explicit #crowd.each loop), so that it would look something like this:
<%= render partial: 'path/to/partial', collection: #crowd, as: :person, day: 'Thursday' %>
(the above doesn't err, but just doesn't assign day in the relevant partial)

There's locals for that:
render partial: 'some_partial', collection: #collection, as: :item_name, locals: { variable_name: 'value', other_variable: 123 }

Related

How to reference the current collection rendering variable in Rails

I have the following collection rendering code:
<%= render partial: 'product_models/attr_val',
collection: attr_vals.sort_by {|attr_val| attr_val.name},
as: :attr_val,
locals: {input_type: "checkbox",
is_input_checked: checked_attr_vals_ids.include?(WHAT_TO_PUT_HERE.id),
is_input_disabled: true} %>
Is there any way I can reference the current object (attr_val) in the locals? If it is possible, what should I replace WHAT_TO_PUT_HERE with?

Rails: Render collection: Getting current variable inside partial

I have a collection with named variable:
= render partial: 'universal_partial', collection: districts, as: :district
Inside partial I want to get current variable:
<li><a><%= current_variable %></a></li>
But I have universal partial, it can gets collection from diffirent places:
= render partial: 'universal_partial', collection: subways, as: :subway
I think, that it would be as:
<li><a><%= local_assigns[as] %></a></li>
How can I get name of current variable (value of key :as) inside partial?
By the 'as' parameter you set the name of your variable. Use the same variable name for all collections:
= render partial: 'universal_partial', collection: districts, as: :variable_name
= render partial: 'universal_partial', collection: subways, as: :variable_name
<li><a><%= variable_name %></a></li>

rails: pass variable to partial

How can I pass a variable to a partial using this code? :
<%= render #performance_indicator.improvement_actions.order("created_at DESC") %>
I want to pass "id=1" and then in _improvement_action, use that variable like:
<%= id %>
EDIT:
This is my improvement_action partial:
https://gist.github.com/luisamaro0/6597084f2de1dc33cde7c014ea9f23a5
You can pass a local variable like so:
render "a_partial", :a_local_variable => whatever, :another_variable => another
See this question for more details: Rails 3, passing local variable to partial
you can pass a variable like this
<%= render partial: 'partial_name', locals: {id: '1'} %>
Try this syntax out:
<%= render #performance_indicator.improvement_actions, locals: { local_variable: 1 %>
# then <%= local_variable %>

Rails passing form_for object to partial

I would like to pass the form_for object to a partial:
<%= form_for #price do |f| %>
...
<%= render :partial => "price_page", :object => #price, :as => :f %>
...
<% end %>
When I call:
f.radio_button
Brings the error:
undefined method `radio_button' for #<Price:0x3cb1ed0>
How can I use f as I usually would in this partial?
Try passing form object as local
<%= render :partial => "price_page", :locals=>{:f=>f} %>
You can pass form builder object as a local variable like below,
<%= form_for #price do |f| %>
<%= render :partial => "price_page", :locals => { :f => f } %>
<% end %>
in your partial file you will be receiving form builder as a local variable "f", you can use like below,
<% f.radio_button, {} %>
I ran across this question trying to figure out how to get a form builder into a partial without an additional form tag. That's the primary use case I could think of for this question, so I'm adding this answer for future visitors.
To solve my problem, I have my form_for in my layout and I render my partial passing only the model. In my partial I use fields_for.
Looks (something) like this:
= form_for #price do |f|
...
= render partial: "price_page", object: #price, as: 'price %>
...
Then, my partial has this:
= fields_for price do |f|
...

Render partial :collection => #array specify variable name

I am rendering a partial like this:
$("#box_container").html("<%= escape_javascript( render :partial => 'contacts/contact_tile', :collection => #contacts) %>")
Problem is that my partial is expecting the variable 'contact'.
ActionView::Template::Error (undefined local variable or method `contact'
I simply want to tell the partial to expect a variable contact. Should iterate through #contacts as contact. How do I do that?
Found this is also helpful from the docs. You aren't limited to having the variable named after the partial:
http://guides.rubyonrails.org/layouts_and_rendering.html
To use a custom local variable name within the partial, specify the
:as option in the call to the partial:
<%= render :partial => "product", :collection => #products, :as => :item %>
With this change, you can access an instance of the #products collection as the item local variable within the partial."
The documentation at http://guides.rubyonrails.org/layouts_and_rendering.html says:
When a partial is called with a pluralized collection, then the
individual instances of the partial have access to the member of the
collection being rendered via a variable named after the partial.
So it will be passed a variable called "contact_tile" instead of "contact". Perhaps you can just rename your partial.
If this naming is important, you could do it explicitly without the collection option by something like:
#contacts.each { |contact| render :partial => 'contacts/contact_tile', :locals => {:contact => contact } }
(although as a commenter pointed out, this may not be as performant)
Latest syntax are :
index.html.erb
<%= render partial: "product", collection: #products %>
_product.html.erb
<p>Product Name: <%= product.name %></p>
#products is used in partial as product
Where #products can be considered as Product.all
and product can be considered as a row of product i.e. Product.first as looped all product one by one.
You can specify a custom variable name as the default with the keyword as:
<%= render partial: 'line_items/line_item', collection: order.line_items, as: :item %>

Resources