Rendering a Tree Using render collection - ruby-on-rails

I have a site with reviews. When displaying them, the first review for an item should not be indented, but subsequent ones should be.
The HTML output should look something like:
<ul>
<li>review for item 1</li>
<li>
review for item 2
<ul>
<li>2nd review for item 2</li>
<li>3rd review for item 2</li>
</ul>
</li>
</ul>
The output I'm getting looks like this:
<ul>
<li>review for item 1</li>
<li>
review for item 2
</li> <!-- wrong -->
<li> <!-- wrong -->
<ul>
<li>2nd review for item 2</li>
<li>3rd review for item 2</li>
</ul>
</li>
</ul>
_reviews.html.erb
<%= render :partial => "review_item", :collection => #reviews, :locals => { :reviews_local => #reviews } %>
_review_item.html.erb
<% if review_item_counter == 0 %>
<ul>
<% end %>
<li>
<% if #current_reviewable != review_item.id %>
<% #current_reviewable = review_item.id %>
<%= review_item.body %>
<% else %>
<% reviews_local.slice!(review_item_counter, reviews_local.size) %>
<%= render :partial => "review_item", :collection => reviews_local, :locals => { :reviews_local => reviews_local } %>
<% end %>
</li>
<% if review_item_counter == reviews_local.size - 1 %>
</ul>
<% end %>

Related

Displaying post base on category in Rails

How can I display the post base in the current category, I can show the category but only one post show.
In my controller:
#area_category = Project.joins(:area_category).group(:area_category_id)
In my view
<% #area_category.each do |category| %>
<%= category.area_category_name %>
<% category.project_title %>
<% end %>
Basically, i want to display category and post on each category
Something like this:
<ul>
<li>Category 1
<ul>
<li>Post 1</li>
<li>Post 2</li>
</ul>
</li>
<li>Category 2
<ul>
<li>Post 1</li>
<li>Post 2</li>
</ul>
</li>
</ul>
controller
#area_category = AreaCategory.preload(:projects)
view
<% #area_category.each do |category| %>
<%= category.area_category_name %>
<% category.projects.each do |project| %>
<%= project.project_title %>
<% end %>
<% end %>

How to avoid same instance variables for footer

I have a footer in my rails application that shows all recent posts and categories produced by two instances variables in the welcome_controller.rb
But then if i go to another section that has a different controller responsible i get an error as the #posts and #categories instance variables are not there to loop through and list the latest posts and categories.
welcome_controller.rb (code extract)
#categories = Category.all
#posts = Post.order("created_at DESC")
_footer.html
<div class="row footer-black">
<div class="large-3 columns text-left">
<h4>Categories</h4>
<ul>
<% #categories.each do |c| %>
<% unless c.header? %>
<% if c.restriction %>
<% if check_for_free_questions_in_restricted_category(c) %>
<li> <%= link_to c.title, category_random_free_question_path(c),'data-no-turbolink' => true %> </li>
<% else %>
<li> <%= link_to c.title, new_subscription_path,'data-no-turbolink' => true %> </li>
<% end %>
<% else %>
<li> <%= link_to c.title, category_random_question_path(c),'data-no-turbolink' => true %> </li>
<% end %>
<% end %>
<% end %>
</ul>
</div>
<div class="large-5 columns text-left">
<h4>Latest Posts</h4>
<ul>
<% #posts.each do |p| %>
<li> <%= link_to truncate(p.title, length:70), blog_post_path(p) %> </li>
<% end %>
</ul>
</div>
<div class="large-4 columns text-left social-links">
<h4>Social</h4>
<a href="#">
<i class="fa fa-facebook-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-twitter-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-google-plus-square fa-2x"></i>
</a>
</div>
</div>
is there a way to show categories and posts in footer without instantiating same variables over and over again?
The short answer is 'no'. Every variable must be instantiated (initialized) before it can be used (in this case, in your view).
Now, there are some more efficient ways of instantiating these variables than writing:
#categories = Category.all
#posts = Post.order("created_at DESC")
in multiple controllers. But, that's a different question. If you're interested in that, then ask a new question or refine this one.

Rails view returning database field after loop

In my view I do this:
<ul class="list-group">
<%= JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
It lists everything out like it should, but then at the end it returns the value of current_printer.stripe_managed_account.fields_needed. I'm not sure why it does this, or how to prevent it from doing this.
This is a screenshot:
Replace <%= %>, on your each line by <% %>.
Because <%= #your each %> is same that : <% puts #your each %>.
Try this:
<ul class="list-group">
<% JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
Thanks #vee

basic counter to increment a link is not displaying properly in my view

I have a number of steps and I have a counter that I would like to increment as the block iterates through the steps. More specifically, if its media type is "excel" or anything else I would like it to say step 2, step 3, step 4 etc. However in the view it only says step 1, step 1, step 1 - it does not properly increment. What is wrong? Any help would be greatly appreciated.
<% #step_list.each do |i| %>
<% x = 0 %>
<% case %>
<% when i.media_type == 'video' %>
<% x += 1 %>
<li class="active">
<span>Video</span>
</a>
</li>
<% when i.media_type == 'excel' %>
<% x += 1 %>
<li class="">
<span>Step <%= x %> </span>
</li>
<% else %>
<% x += 1 %>
<li class="dark-nav ">
<span>Step <%= x %></span>
</li>
<% end %>
<% end %>
The problem in your code is that you initialize the variable x in each loop, you should initialize it to zero before your loop.
To solve this, You can refactor this with the method .each_with_index do |element, i|:
<% #step_list.each_with_index do |step, i| %>
<% case step.media_type %>
<% when 'video' %>
<li class="active">
<span>Video</span>
</li>
<% when 'excel' %>
<li class="">
<span>Step <%= i %></span>
</li>
<% else %>
<li class="dark-nav ">
<span>Step <%= i %></span>
</li>
<% end %>
<% end %>
Why don't you use each_with_index ?
[1, 2, 3].each_with_index do |n, i|
end
You reset your x each time, try that :
<% #step_list.each_with_index do |i,index| %>
<% case i.media_type %>
<% when 'video' %>
<li class="active">
<span>Video</span>
</a>
</li>
<% when 'excel' %>
<li class="">
<span>Step <%= index %> </span>
</li>
<% else %>
<li class="dark-nav ">
<span>Step <%= index %></span>
</li>
<% end %>
<% end %>
<% end %>

ruby "first" helper method?

i have the following file member.html.erb:
<ul class="members-list">
<%#members.each do |member|%>
<li class="member-item">
<%=member.name%>
</li>
<%end%>
</ul>
i want to add the "first" class to the first li that will be generated (just to the first), so it would look like this:
<ul>
<li class="member-item first">john</li>
<li class="member-item">chris</li>
</ul>
any thoughts?
You could acheive this using CSS aswell if its purely for the styling. If you either has a ID or class on the ul then you could in CSS write
ul.member-list li:first-child {
WHATEVER STYLING CODE THAT YOU WOULD PUT IN member-item first
}
and for the rest of the li-tags
ul.member-list li {
}
If you want the class to be added you could e.g
<ul>
<% #members.each_with_index do |member, i| %>
<% if i == 0 %>
<li class="member-item first">
<% member.name %>
</li>
<% else %>
<li class="member-item">
<% member.name %>
</li>
<% end %>
</ul>
Similar, but more terse, approach to Andreas/Heikki's answer:
<ul>
<% #members.each_with_index do |member, i| %>
<li class="member-item<%= ' first' if i == 0 %>">
<% member.name %>
</li>
<% end %>
</ul>

Resources