I am having difficulty passing instance variables in nested partials. Here is what I have done.
In controller Own:
def home
#item = "some values"
#ref = "some other values"
end
Then I have a home page "home.html.erb", in which I rendered "_product_table.html.erb":
<%= render "own/product_table", :item => #items, :ref => #ref %>
Then, in "_product_table.html.erb", I have to render "_product.html.erb":
<% #items.each do |item|%>
<%= render "own/product", :item => item, :ref => #ref %>
<% end %>
I can't understand why the ref variable is not available in the "_product.html.erb" partial.
You are passing ref as argument to the partial in home.html.erb, so in _product_table.html.erb you should use it similarly to item, not as instance variable:
<% #items.each do |item|%>
<%= render "own/product", :item => item, :ref => ref %>
<% end %>
Related
Here is the code
Model:
Category has many subjects
View:
show:
<% category.subjects.each do |subject| %>
<div class="container">
<%= render partial: "layouts/trial", :locals => {:subject => subject} %>
</div>
<% end %>
layouts/trial:
<%= description(#subject) %>
trial_helper.rb
module TrialHelper
def subject
#subject ||= []
end
def description(subject)
#des = "#{subject.content}"
end
end
turn out
ActionView::Template::Error (undefined method `content' for nil:NilClass):
1: <%= description(#subject) %>
I've tried using
<%= render partial: "layouts/trial", :locals => {:subject => #category.subject} %>
and
def description(subject)
#des = "#{#subject.content}"
end
But it's still not work. What is the problem?
Probably your subject is nil in your layouts/trial.
The best approach is to debug and check where the problem is occurring.
You can set in your layouts/trial something like <%= raise #subject.inspect %> and if it really returns nil, we can confirm that this code here <%= render partial: "layouts/trial", :locals => {:subject => subject} %> isn't working as expected.
change
<%= description(#subject) %>
to
<%= description(subject) %>
locals are passed as local variables not instance variables
I'm populating a partial with many instance variables in the locals hash. Is there a way to pull multiple instance variables from local_assigns in the partial?
<%= render 'tabs', :locals => {:tab1 => #news, :tab2 => #people, :tab3 => #tags} %>
In the partial I'd like to create a dynamic set of tabs based on the number of instance variables I populate the locals with.
<% local_assigns.each do |local_assign| %>
<% local_assign.each do |tabs| %>
<!-- Grab tab class name -->
<li class="tab"><%= tabs[:class] %></li>
<% end %>
<% end %>
Why don't you just pass it as an array
render 'tabs', :locals => {:tabs => [#news, #people, #tags]} %>
in your view
<% tabs.each do |tab| %>
<!-- Grab tab class name -->
<li class="tab"><%= tab[:class] %></li>
<% end %>
Thanks Vimsha I ended up using collection instead of locals and created the instance variable in the controller.
http://guides.rubyonrails.org/layouts_and_rendering.html
Controller
#tabs = [#news, #people]
View
<%= render 'tabs', :collection => #tabs %>
Right now I have a rails partial that looks like this:
<%= render :partial => "/talk/partials/comment", :collection => #comments, :locals => {:votes => #votes} %>
I am passing in a collection of comments and another local variable.
That comment partial then goes right into using the comment variable and works fine.
I have since made another partial called '/talk/partials/comment_2014'. When I try this, I am getting the error undefined local variable or method 'comment'. From what I can gather, when I have a different partial name, something with the variable also changes. I would like to keep the same comment variable for the new partial ''/talk/partials/comment_2014'. How would I go about doing this?
Something I tried which did not work was the following:
<% #comments.each do |comment| %>
<%= render :partial => "/talk/partials/comment_2014", comment: comment, :locals => {:votes => #votes} %>
<% end %>
which did not work either.
You can do it this way
<% #comments.each do |comment| %>
<%= render "/talk/partials/comment_2014", comment: comment, votes: #votes %>
<% end %>
Or
<% #comments.each do |comment| %>
<%= render partial: "/talk/partials/comment_2014", locals: { comment: comment, votes: #votes } %>
<% end %>
Notice in the second way the comment is inside the locals.
I'm developing a rails application.
I want the user to be able to make a selection between an array of models
In one controller, I create an array of models.
def myController
#data = []
#data += [MyData.find(2)]
#data += [MyData.find(5)]
#data += [MyData.find(7)]
end
In the view, I can't use the form_for because can't be used in an array, so I have:
<%= form_tag 'myOp' do |f|%>
<%= fields_for :test, #data do |builder|%>
<%= render 'sub_form', :f => builder %>
<% end %>
<% end %>
Now in the sub_form, I want to recieve each of the items of the array, but instead, I'm getting the full array.
How can I get each of the items of the array in the subform?
Is there a better way to do this?
Thanks
So first in your controller
def my_action
#datas = MyData.find(2, 5, 7)
end
Then in your view
You need to iterate through the #datas array and yield the fields for each object. That is because fields_for yields fields for one object only, not arrays of objects.
<%= form_tag 'myOp' do |f|%>
<% #datas.each_with_index do |data, i| %>
<%= fields_for "test_#{i}", data do |builder|%>
<%= render 'sub_form', :f => builder %>
<% end %>
<% end %>
<% end %>
I hope this will correct the issue:
<%= form_tag 'myOp' do |f|%>
<%= fields_for :test, #data.each do |builder|%>
<%= render 'sub_form', :f => builder %>
<% end %>
<% end %>
Normally an array object can be seperated using .each method. May that would work here also. Try it.
I can't seem to access instance objects in partials. Example:
In controller I have:
ActiveAdmin.register Store do
sidebar "Aliases", :only => :edit do
#aliases = Alias.where("store_id = ?", params[:id])
render :partial => "store_aliases"
end
end
Then in the _store_aliases.html.erb partial I have:
<% #aliases.each do |alias| %>
<li><%= alias.name %></li>
<% end %>
This doesn't work. The only thing that does work (which is horrible to do as I'm putting logic in a view is this:
<% #aliases = Alias.where("store_id = ?", params[:id]) %>
<% #aliases.each do |alias| %>
<li><%= alias.name %></li>
<% end %>
When rendering a partial you need to actively define the variables that are given to that partial. Change your line
render :partial => "store_aliases"
to
render :partial => "store_aliases", :locals => {:aliases => #aliases }
Inside your partial the variables is then accessible as a local variable (not an instance variable!). You need to adjust your logic inside the partial by removing the #.
<% aliases.each do |alias| %>
<li><%= alias.name %></li>
<% end %>
For further reading have a look at the API documentation (specifically the section "3.3.4 Passing Local Variables").
You have to pass your instance variable to your partial in order to use it there:
ActiveAdmin.register Store do
sidebar "Aliases", :only => :edit do
#aliases = Alias.where("store_id = ?", params[:id])
render :partial => "store_aliases", :locals => { :aliases => #aliases }
end
end
Then in your partial you will be able to use it as local variable
<% aliases.each do |alias| %>
<li><%= alias.name %></li>
<% end %>