This is similar to this question but with a collection:
<div class="panel-body">
<%= render layout: 'today_items_list', locals: {items: #pos} do |po| %>
<% #output_buffer = ActionView::OutputBuffer.new %>
<%= link_to "##{po.id}", po %>
<%= po.supplier.name %>
<% end %>
</div>
with partial/layout:
.tableless_cell.no_padding
%h3.no_margin_vertical= title
%ul.no_margin_vertical
- for item in items
%li= yield(item)
This renders as you expect but if I omit the weird '#output_buffer = ActionView::OutputBuffer.new', the buffer is not cleared and the list is rendered this way:
<li>
#4833Supplier name
</li>
<li>
#4833Supplier name
#4835Supplier name 2
</li>
<li>
#4833Supplier name
#4835Supplier name 2
#4840Supplier name 3
</li>
Never clearing the buffer between block invocation. What am I missing here?
(Riding on Rails 3.2.22)
I don't have much experience with Rails 3.2; with newer versions, you're able to pass collections to partials:
<%= render #pos, layout: 'today_items_list' %>
#app/views/pos/_today_items_list.html.erb
.tableless_cell.no_padding
%h3.no_margin_vertical= title
%ul.no_margin_vertical
<%= yield %>
#app/views/post/_po.html.erb
<%= link_to "##{po.id}", po %>
<%= po.supplier.name %>
You can read more about collections etc (I presume for Rails 4+) here.
Related
Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>
I'm following along a tutorial inn rails in which we can go the specific branch's category page but i m getting an error that the path sent on click event is not recognized by the app
the code for that page where i used that variable is
<% branch_path_name = "#{params[:action]}_posts_path " %>
<div class="col-sm-12">
<ul class="categories-list">
<%= render all_categories_button_partial_path, branch_path_name: branch_path_name %>
<%= #categories.each do |category | %>
<li class="category-item">
<%= link_to category.name, send(branch_path_name, category: category.name), :class => ("selected-item" if params[:category] == category.name) %>
</li>
<% end %>
</ul>
</div>
my all_categories_button_partial helper method is as follow
def all_categories_button_partial_path
if params[:category].blank?
'posts/branch/categories/all_selected'
else
'posts/branch/categories/all_not_selected'
end
end
and the partial i used inside my helper method are as _all_selected.html.erb
<li class="category-item">
<%= link_to "All", send(branch_path_name), :class => "selected-item" %>
</li>
and _all_not_selected.html.erb partial is
<li class="category-item">
<%= link_to "All", send(branch_path_name) %>
</li>
but when i click on the link then i got this error
undefined method `hobby_posts_path ' for #<#<Class:0x000000000e32de40>:0x000000000d3e1810>
Did you mean? hobby_posts_path
hobby_posts_url
Extracted source (around line #2):
1
2
3
<li class="category-item">
<%= link_to "All", send(branch_path_name), :class => "selected-item" %>
</li>
where i have done wrong?
Your path definition has an extra space. It currently is <% branch_path_name = "#{params[:action]}_posts_path " %>, while it should be <% branch_path_name = "#{params[:action]}_posts_path" %> (Notice the space is deleted after path)
I'm trying to render the following partial:
<% #accepted.each do |question| %>
<div class="questions-container__content">
<div class="questions-container__accepted-content">
...
</div>
<%= render 'question_buttons', collection: #accepted %>
</div>
<% end %>
with _question_buttons.html.erb:
<div class="links-container__button-group" id="link-buttons">
<%= link_to "View submission", coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<%= link_to "Edit", edit_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% if !question.accepted? %>
<%= link_to "Activate" , activate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% else %>
<%= link_to "Deactivate" , deactivate_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
<% if current_user.admin? %>
<%= link_to (question.rejected ? "Restore" : "Reject"), reject_coin_question_path(question.coin, question.id), class: "primary-small","data-turbolinks"=>"false" %>
<% end %>
</div>
I get the following error:
undefined local variable or method `question' for #<#<Class:0x00007fece6998d08>:0x00007fed02072bb8>
What am I doing wrong here?
I believe the issue is that you need to pass the question variable from each loop in the parent view through to the partial using locals which allows the partial to access it.
<%= render 'question_buttons', locals: { question: question } %>
First:
When rendering a collection, each item of the collection is passed to the partial as a local variable with the same name as partial itself. It means that for this call:
<%= render 'question_buttons', collection: #accepted %>
the partial question_buttons will be called for each item of #accepted array; that item will be available inside partial as question_buttons.
If you want to use another name for the item, for example question, you need to call it as:
<%= render 'question_buttons', collection: #accepted, as: :question %>
Another option -- just rename the partial to question:
<%= render 'question', collection: #accepted %>
Second:
In your code snippet rendering collection is called on each iteration of the loop over #accepted elements. If #accepted has 8 elements, for example, the partial will be rendered 8 times for each of these elements, ie 8 * 8 = 64 times in total. I suspect that it's not what you want to achieve. Your code looks like question_buttons partial needs to be rendered for each element of #accepted only once. In this case using collection param has no sense here. Just pass a local variable question into the partial:
<%= render 'question_buttons', question: question %>
I have a common problem that normally I would solve with locals, but this time wont work.
I have the following block:
<% #user.followers.each do |follower| %>
<%= render "follower_card" %>
<% end %>
And the following partial:
<div class="row follower-card">
<%= image_tag follower.avatar_url(:smallthumb), class: "col-4 inline avatar_medium circle" %>
<ul class="col-8 inline">
<li><%= follower.name %></li>
<li><%= follower.location %></li>
<li class="lightgray small inline"><span class="bold"><%= follower.photos.count %></span> Spots</li> -
<li class="lightgray small inline"><span class="bold"><%= follower.albums.count %></span> Spotbooks</li>
</ul>
</div>
I'm getting the following error:
undefined local variable or method `follower' for #<#<Class:0x007fe791a4c8d0>:0x007fe799b14b98>
This should work (specifying the follower variable):
<%= render "follower_card", follower: follower %>
Anyway, I recommend you to use collection rendering for performance reasons. Take a look here: http://guides.rubyonrails.org/layouts_and_rendering.html. Should be something like:
<%= render "follower_card", collection: #user.followers %>
Related question: Render partial :collection => #array specify variable name
Note
Old syntax to pass variables to partials is also valid (verbose, but less elegant IMHO):
<%= render partial: "follower_card", locals: { follower: follower } %>
This is because you are not passing the variable to the partial. The scope of the partial is limited to itself, and you are not making follower available inside. You will have to use:
<% #user.followers.each do |follower| %>
<%= render "follower_card", locals: {follower: follower} %>
<% end %>
Is the proper way.
<%= render "follower_card", follower: follower %>
or
<%= render partial: "follower_card", locals: {follower: follower} %>
So being new to rails I seem to be stuck on creating a loop within a loop to process the information.
I am getting:
can't convert Symbol into Integer line #11
The line in question is:
Host <%= servicedetails[:hostidn] %> - <%= servicedetails[:status] %>
And here is the full version below. Being new Im clueless and open to suggestions.
<div>
<% #service_hash[:service_list].each do |servicesinfo| %>
<ul>
<li>
<ul>
<li>
<h2><%= servicesinfo[:service_name] %><h2>
</li>
<% servicesinfo.each do |servicedetails| %>
<li>
Host <%= servicedetails[:hostidn] %> - <%= servicedetails[:status] %>
</li>
<% end %>
</ul>
</li>
</ul>
<% end %>
</div>
the JSON equivalent of this hash is
{"status":"successful","service_list":[{"service_name":"oozie","status":"RUNNING","status_message":"Running Master Service","host":"1"},{"service_name":"single-namenode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"single-database","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"datanode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"secondarynamenode","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"web","status":"DEAD","status_message":"Running Master Service","host":"1"},{"service_name":"tasktracker","status":"RUNNING","status_message":"Running Service","host":"1"},{"service_name":"jobtracker","status":"RUNNING","status_message":"Running Master Service","host":"1"}]}
You're already iterating over the array of hashes with service info (renamed to make sense):
<% #service_hash[:service_list].each do |service_info| %>
...
<% end %>
Iterating over service_info would return [key, value] pairs--likely not what you want.
Access the information from service_info directly, as you already do with :name
<%= service_info[:host] %> - <%= service_info[:status] %>
I don't see anything called :hostidn in that hash, just :host; not sure if that's a typo, or if you're expecting additional data not shown.