Multiple partials inside a content_for? - ruby-on-rails

I have various 'sidebar modules' in different partials so that different layouts, etc. can have different sidebar modules. So I have this in my layout file:
<div id="sidebar">
<%= yield :sidebar %>
</div>
and in different views I use:
<% content_for :sidebar do %>
<% render :partial => '/sidebar_modules/allergy_season' %>
<% end %>
This works perfectly fine, as expected. Unfortunately, I'm unable to render multiple partials this way. For instance:
<% content_for :sidebar do %>
<% render :partial => '/sidebar_modules/allergy_season' %>
<% render :partial => '/sidebar_modules/resolution_rate' %>
<% end %>
will not work. It will just render the last one.
What would be the easiest/cleanest way to achieve this?

Try using <%= instead of <%
<% content_for :sidebar do %>
<%= render :partial => '/sidebar_modules/allergy_season' %>
<%= render :partial => '/sidebar_modules/resolution_rate' %>
<% end %>

Related

how to render multiple js.erb files in same method in rails?

def react
#postsAlPHA = #user.find(params[:liked_post]).page(params[:page])
#postsBETA = #user.find(params[:disliked_post]).page(params[:page])
end
so I am trying to make #postsAlPHA to render alpha.js.erb for pagination
and #postsBETA to render
BETA.js.erb
<% #postsAlPHA.each do |x| %>
<% x.body %>
<% end %>
<%= paginate #postsAlPHA %>
<% #postsBETA.each do |u| %>
<% x.content %>
<% end %>
<%= paginate #postsBETA %>
Turn them into partials, then render them in your view. Make sure to prefix the filenames with _ e.g. _alpha.js.erb.
In your view:
<%= render partial: 'path/to/alpha.js.erb' %>
<%= render partial: 'path/to/beta.js.erb' %>

Rails: How to use same partial file for one to many relationship?

For the normal situation. Different views can use the same partial like this
<h1>New zone</h1>
<%= render partial: "form", locals: {zones: #zones} %>
<h1>Editing zone</h1>
<%= render partial: "form", locals: {zones: #zones} %>
<% zones.each do |zone| %>
<%= zone.location%>
<% end %>
But my situation is a bit different, the origin code like this
one belongs_to two #one = One.all
one.html.erb
<%= render partial: "one",locals:{one: #one} %>
_one.html.erb
<% one.each do |n|%>
<%= n.location%>
<% end %>
two has_many one #two = Two.all
two.html.erb
<%= render partial: "two", #two %>
_two.html.erb
<% #two.each do |one|%>
<% one.each do |n|%>
<%= n.location %>
<% end %>
<% end %>
For this situation, how can I code render for two.html.erb if I want one.html.erb and two.html.erb use the same partial _one.html.erb ?
this one is definitely incorrect, but how to correct this?
two.html.erb
<%= render partial: "one/one", locals: {one: #two} %>
========================update============================
I think the basic of question is whether we can use the same partial for an object and at the same time for a collection.

trying to render a partial based on whether it's the home page "/"

I thought this bit of code might work, but...it didn't Any ideas?
<% if "/"? %>
<%= render 'layouts/homeHeader' %>
<% else %>
<%= render 'layouts/header' %>
<% end %>
According to this answer you can do this:
<% if current_page?(root_url) %>
<%= render 'layouts/homeHeader' %>
<% else %>
<%= render 'layouts/header' %>
<% end %>

Refactoring into partials in Rails

In page1, I use code A+B+C
In page2, I use code B+C
So when I make a partial, I realy have no idea in how to deal with this.
For example, In a Post-Comment system. I want to show #comments in 2 different pages. In the comment index page,
We show the post it belongs to. And in the post show page, We only have to show the comments content.(Since there is no need to show the comment.post again)
#Comment Index Page
<% #comments.each do |comment| %>
<%= comment.post %>
<%= comment.author %>
<%= comment.content %>
<% end %>
..
#Post Show Page
<% #comments.each do |comment| %>
<%= comment.author %>
<%= comment.content %>
<% end %>
So, how do I make a partial to reuse the code? Perhaps like this? But this there more elegant way of doing this?
#Comment Index Page
<% #comments.each do |comment| %>
<%= comment.post %>
<%= render comment %>
<% end %>
#Post Show Page
<% #comments.each do |comment| %>
<%= render comment %>
<% end %>
Updated:
I adopt the local variable approach, and update my code like:
# partial
<% if include_topic %>
<div class="Topic">
<h5><%= link_to "#{comment.topic.content}", comment.topic %></h5>
</div>
<% end %>
#Index
<%= render #comments, :locals => {:include_topic => true } %>
But I get undefined local variable or method `include_topic' for #<#
I just find nowhere to debug this issue
Your partial:
<%= comment.post if include_post %>
<%= comment.author %>
<%= comment.content %>
your code:
#index page
<%= render :partial => "partial_path", :collection => #comments, :as => :comment, :locals => {:include_post => true } %>
#show page
<%= render :partial => "partial_path", :collection => #comments, :as => :comment, :locals => {:include_post => false } %>
Syntax could be much shorter but it depends whether or not you stick to rails conventions see doc.
Sidenote: I don't like 1.8.7 syntax
In the partial,
<% comments.each do |comment| %>
<%= comment.post if params[:controller] == "comments" %>
<%= comment.author %>
<%= comment.content %>
<% end %>
and now render this partial in both comments/index and posts/show pages by specifying the comments as a local variable.

How can I pass more than 2 arguments to partial?

I want to pass 2 arguments such as topic and icon_photo.
How can I do that?
undefined method `icon_photo?'
I got this error with the code below.
view
<div class="Topic">
<% #community.topics.each do |topic| %>
<%= render 'topics/topic', :topic => topic, :icon_photo => topic.user.profile.avatar %>
<% end %>
</div>
You can pass a locals hash:
<div class="Topic">
<% #community.topics.each do |topic| %>
<%= render 'topics/topic', locals: {topic: topic, icon_photo: topic.user.profile.avatar, etc: 'blabla' } %>
<% end %>
</div>
See some documentation here:
http://www.tutorialspoint.com/ruby-on-rails/rails-render.htm
A little improvement can be mabe, you can render your collection like this:
<div class="Topic">
<%= render partial: 'topics/topic', collection: #community.topics %>
</div>
# in your partial topics/_topic.html.erb:
<% icon_photo = topic.user.profile.avatar %>

Resources