Using partials on arrays? - ruby-on-rails

I'm confused by how partials behave with respect to arrays.
Simple Example
I have the following in a view:
render :partial => "foobars", :object => [1, 2, 3]
And in _foobars.html.erb, I have
<%= foobars.size %>
<%= foobars[0] %>
The weird thing is that what gets displayed is "444" and "101", not "3" and "1".
Is something special happening because I'm passing in an array?

What Jed says works but what you are looking for is really
render :partial => "foobars", :collection => [1,2,3]
Inside the partial, the iteration will happen by itself on the passed array and foobars will hold the array element of each iteration
<%= foobars %>
will give 1, 2 and 3 inside the partial.

I think what you want is:
render :partial => "foobars", :locals => {:object => [1, 2, 3]}
and inside the partial
<%= object.size %>
<%= object[0] %>

Related

How can I use 'in-page link' by # when using Kaminari?

I have this in my view
<%= paginate #comments, :window => 4, :outer_window => 2 %>
This automatically creates pagination links. If I click one of them, it takes me to
http://example.com/shop/walmart?page=2
How can I add in-page link, which takes me to this url?
http://example.com/shop/walmart?page=2#abc
Supposing #abc is the destination here.
<%= paginate #comments, :window => 4, :outer_window => 2 , :params => {:anchor => "abc"} %>

How to pass another flag variable in addition to collections to a partial?

How can I pass a is_featured = true to the following partial?
<%= render :partial => 'stores', :collection => #stores %>
I only need to pass the is_featured in one place (in all other places I call the partial as above.
You can use the locals option of render
<%= render :partial => 'stores', :collection => #stores, :locals => { :is_featured => is_featured } %>
In the partial you would access it as a method:
<%= is_featured %>

Local variable not being passed to partial template by render?

I don't seem to be able to pass a variable to my partial template in rails (2.3.5). My code is as follows;
In the main view .html.erb file:
<% f.fields_for :payments do |payment_form| %>
<%= render 'payment', {:f => payment_form, :t => "test" } %>
<% end %>
and in the _payment.html.erb file:
<%= t %>
produces a wrong number of arguments (0 for 1) error. The payment_form object is being passed to the partial as f without any problems. I've tried a number of variations on the above syntax (e.g. :locals => {:f => payment_form, :t => "test" } without success. I presume I'm doing something pretty basic wrong but just can't see it.
It's probably because t() is a reserved view helper method used for I18n. Just rename it to something more descriptive
Try
render :partial => 'payment', :locals => {:t => 'test'}
Have you tried
<%= render 'payment', :f => payment_form %>
I'm not sure what the :t is for, but rails is obviously saying that you should only be passing in one extra parameter with the wrong number of arguments (0 for 1) error.

Rails partial locals not persisting when sent to another partial as its own local

I render a partial like so:
<%= render :partial => 'widgets/some_partial, :locals => {:foo => 'bar'} %>
So inside of _some_partial.html.erb I render two more partials like so:
<% #foo.nil? #=> false %>
<%= render :partial => 'widgets/another_partial', :locals => {:foo => foo} %>
`<%= render :partial => 'widgets/another_partial_again', :locals => {:foo => foo} %>`
The foo local variable renders fine in some_partial.html.erb and even in another_partial_again.html.erb. However, the foo variable is inaccessible in another_partial.html.erb even though I explicitly passed it in the render call.
What is happening here?
Thanks for the help.
I had the undefined local variable or method error come up for me too when I was rendering a partial with :locals defined.
However, I had a different issue causing my problem, so I thought I would share my solution in case it helps anyone else. (This page was the first result when I googled this error after all)
Basically just make sure you use :partial => 'path/to/partial' in your call to render.
I.e.
<%= render :partial => 'widgets/some_partial', :locals => {:foo => 'bar'} %>
NOT like I was doing:
<%= render 'widgets/some_partial', :locals => {:foo => 'bar'} %>
Easy for a rails/ruby newbie like me to miss.
Solved. Turns out I was also rendering the same partial from the controller without sending the proper local variables. Thanks anyways!!!
Bumped into this very old question cause I faced the same issue.
Turned out that with Rails 4+ if you are not using collections or layout the correct way is:
# Instead of <%= render partial: "account", locals: { account: #buyer } %>
<%= render "account", account: #buyer %>
As documented here.

Multivariable Partial in Ruby on Rails

I have a partial that I want rendered with a collection and another variable. Is it possible to pass more than one variable to a partial?
To illustrate:
Category HABTM Brands
This is just semi-pseudo-code, but I want to do something like:
<% #categories.each do |c| %>
<%= c.name %>
<%= render :partial => "mypartial", :collection => c.brands, :object => c.id %>
<% end %>
The partial needs the category id as well as the "current_brand". Any ideas?
Inside of your view, you pass a hash to the :locals key-value pair in the options hash argument.
<%= render :partial => 'partial', :locals => { :foo => 'a', :bar => 'b' } %>
... and these keys become available as variables in your partials.
Foo is: <%= foo %>
Bar is: <%= bar %>
You can give a partial any number of variables with the :locals option. It takes a hash of variable names and values.

Resources