Rails: Render collection: Getting current variable inside partial - ruby-on-rails

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>

Related

Can you pass extra variables when rendering a collection in 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 }

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 partial collection as not working

I am trying to render a partial of a collection using a name different to that of the model:
<%= render "current_campaigns", collection: #current_campaigns, as: :current_campaign %>
The model is called Campaign but this is a subset of campaigns as defined in the controller action:
def index
#current_campaigns = Campaign.where(status: :approved)
end
In the partial (which lives in the application directory not the campaigns directory):
<%= current_campaign.question %>
The resulting error:
undefined local variable or method `current_campaign' for #<#<Class:0x007fad3d5e5500>:0x007fad451976a8>
I was under the impression that as would make this work but apparently not. Any thoughts?
Try the following
<%= render partial: 'path-relative-to-views/current_campaigns', collection: #current_campaigns, as: :current_campaign %>
And the partial should name _current_campaign.html.erb for convention.
The most simple way to pass attributes to the partial using a custom name is:
<%= render "current_campaigns", custom_name: #current_campaigns %>
doing this you will be able to use custom_name as a defined variable on your partial.
Try to rename campaigns/current_campaigns.html.erb template to campaigns/campaign.html.erb and render like that from campaigns/:
<%= render #current_campaigns %>
Otherwise you can specify name of directory where the partial is:
<%= render "other_folder_name/campaign", collection: #current_campaigns %>

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 %>

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