Displaying post base on category in Rails - ruby-on-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 %>

Related

How to iterate through my array within a hash

Firstly, I am hoping my understanding is correct and I do in fact have a an array within my hash.
I've gotten so far but cant seem to iterate through each hash and its array. What I would like to do is have each tournament grouped by date (year only) and have all its tournaments listed underneath.
So far my logic is
Controller
#tournaments = Tournament.all
#tournament_year = #tournaments.group_by {|y| y.tourn_date.year}
View
<ul class="resp-tabs-list">
<% #tournament_year.each do |y, t| %>
<li><%= y %></li>
<% end %>
</ul>
hash
{2014=>[#<Tournament id: 3, name: "WTS ", tourn_date: "2014-04-26", tourn_location: "Cardiff", created_at: "2014-04-26 14:57:21", updated_at: "2014-04-26 14:57:21">, #<Tournament id: 4, name: "CTS Nottingham", tourn_date: "2014-05-26", tourn_location: "Nottingham", created_at: "2014-04-26 14:57:39", updated_at: "2014-04-26 14:57:48">]}
My desired output would be below
<h3>2014</h3>
<ul>
<li>Tournament Name 1</li>
<li>Tournament Name 2</li>
</ul>
<h3>2013</h3>
<ul>
<li>Tournament Name 1</li>
<li>Tournament Name 2</li>
</ul>
Change your view as below:
<ul class="resp-tabs-list">
<% #tournament_year.each do |y, t| %>
<h3><%= y %></h3>
<ul>
<% t.each_with_index do |tournament, i| %>
<li><%= tournament.name %> <%= i %></li>
<% end %>
</ul>
<% end %>
</ul>
I would suggest
<% #tournament_year.each do |y, t| %>
<h3> <%= y %></h3>
<ul class="resp-tabs-list">
<% t.each do |tournament| %>
<li><%= "#{tournament.to_s}" %></li>
<% end %>
</ul>
<% end %>
where
#app/models/tournament.rb
def to_s
"#{name} #{id}"
end
Ive just come up with this solution as an example, seems to work, can anyone see any reason why this would be incorrect?
<ul class="resp-tabs-list">
<%= #tournament_year.each do |y, t| %>
<li><%= y %></li>
<%= #tournament_year[y].each do |x| %>
<li><%= x.name %></li>
<% end %>
<% end %>
</ul>

how to display an item once from a list where that item shows up multiple times in ruby

I've written the code in my .html.erb file but am thinking it might more sense to write it in my controller? This code returns all the names. I'm trying to figure out how to fetch a tag name only once and if a user inputs a tag name that has already been used it does not show up again.
<ul>
<% #pictures.each do |pic| %>
<% pic.tags.each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
<% end %>
</ul>
<ul>
<% #pictures.collect{|x| x.tags.uniq}.flatten.uniq.each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
</ul>
Using Enumerable#flat_map and Array#uniq:
<ul>
<% #pictures.flat_map { |pic| pic.tags.map(&:name) }.uniq.each do |name| %>
<li>
<%= name %>
</li>
<% end %>
</ul>
Example
<ul>
<% #pictures.each do |pic| %>
<% pic.tags.uniq_by(&:name).each do |tag| %>
<li>
<%= tag.name %>
</li>
<% end %>
<% end %>
Use the Array#- method for all items collected that the user enters.
this is relative to how you store the tag values, inside your save method you should make the tags array to be uniq.

Rendering a Tree Using render collection

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 %>

Rails ancestry treemenu help

I am trying to make a simpel treelistmenu with ancestry.
Here is my code in view and it is not working:
<ul>
<% for cat_opg in CatOpg.roots %>
<li> <%= cat_opg.navn %><li>
<% for cat_opg in CatOpg.children %>
<li> <%= cat_opg.navn %><li>
</ul>
<% end %>
</ul>
<% end %>
And my controller:
def liste
#cat_opg = CatOpg.find(:all)
end
I want to make this simpel tree menu as:
Root
-children
Root
-children
I dont know what i am doing wrong.
PS: I am a rails beginner
First of all, you are getting to model in view, not to local variable.
Second, you're rewriting variable.
It should be something like this:
<ul>
<% cat_opg.roots.each do |cat_opg_root| %>
<li> <%= cat_opg_root.navn %><li>
<% cat_opg_root.children each do |cat_opg_child| %>
<li> <%= cat_opg_child.navn %><li>
</ul>
<% end %>
</ul>
<% end %>
Alex thank you for your answer.
Now it works
Controller: #cat_opg = CatOpg
And the view:
<ul>
<% #cat_opg.roots.each do |cat_opg_root| %>
<li> <%= cat_opg_root.navn %></li>
<% unless cat_opg_root.children.empty? %>
<ul>
<% cat_opg_root.children.each do |cat_opg_child| %>
<li> <%= cat_opg_child.navn %></li>
<% end %>
</ul>
<% end %>
<% end %>
</ul>

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