I'm using rails 4. And i'm trying to output menu navigation. I got tble PlaceType with category and title columns. There are 6 categories and i want to output Category only 1 time and then output all titles, that brlong to this ategory. How can i achieve that?
<% #types = PlaceType.all %>
<% #types.each do |type| %>
<ul><%= type.category %>
<li><%= type.title %></li>
</ul>
<% end %>
That's my way of sloving this problem:
<% #types = PlaceType.all %>
<% #types.group_by(&:category).each do |category, type| %>
<ul><%= category %>
<% type.each do |t|%>
<li><%= t.title %></li>
<% end %>
</ul>
<% end %>
Use the group_by method available in Array.
#types.group_by {|type| type.category}
Will give you a hash that looks something like this:
{
"Category A" => ["Title 1", "Title 2"],
"Category B" => ["Title 3"],
"Category C" => ["Title 4", "Title 5"]
}
Now you can loop through the resulting hash instead, to display titles under each category.
Related
I have an array of objects, where I have the following fields: title, name e url
My data:
#array = [
{ id: 1, title: '123', name: 123 },
{ id: 2, title: '123', name: 321 },
{ id: 3, title: '1234', name: 123 },
]
I need to group my data by title and then make the map with the names
<%= #array.map do |x| %>
<div class='col-md-12'>
<h3><%= x.title %></h3>
<div class="row">
<%= #array.filter do |y| %>
<div class="col-4">
<% if y.title === x.title %>
<p><%= t.name %></p>
<% end %>
</div>
<% end.join.html_safe %>
</div>
</div>
<% end.join.html_safe %>
The return is:
title = '123'
name = '123'
name = '321'
title = '123'
name = '123'
name = '321'
title = '123'
name = '123'
That is, my array comes duplicated ... how to proceed?
Remembering that I am inside the rails view
You're getting duplicate entries since you're mapping over all the elements and filtering the array for every element inside the block.
You can use Enumerable#group_by to group the array elements by title. You can then iterate over the groups and display the title and names of each group. Note that I'm using item[:name] to access the values from the hash since your question mentions an array of hashes. You should update it to item.name (and other places using hash access) if you're working with objects instead.
#groups = #array.group_by {|i| i[:title] }
<% #groups.each do |title, items| %>
<h3><%= title %></h3>
<% items.each do |item| %>
<p><%= item[:name] %></p>
<% end %>
<% end %>
I'd like to create a todo list looking for any cell that matches today's date on either agreement, start or due date.
<% #project.where("project_date_agreement + project_date_start + project_date_due > ?", Date.today).each do |tasks_today| %>
<ul>
<li>Item Due Today</li>
</ul>
<% end %>
Any help getting me in right direction would be appreciated.
You want to use OR conditions in your where clause. Properly speaking this would be in your controller.
#projects = Project.where('date_agreement = ? OR date_start = ? or date_due = ?', Date.today, Date.today, Date.today)
In your Project Model you might want to create a field that say's what's due...
def due_because
due_array = []
due_array << 'Agreement date' if date_agreemnt == Date.today
due_array << 'Start date' if date_start == Date.today
due_array << 'Due date' if date_due == Date.today'
due_array.join(', ')
end
Then in your view you would iterate over the #projects
<ul>
<li>Items Due Today</li>
<% #projects.each do |project| %>
<li><%=project.name%> <%=project.due_because%></li>
<% end %>
</ul>
if you asking one row and different column, how about if you combine with table
<% #tasks = #projects.where("date_agreement = ? AND date_start = ? AND date_due = ?",Date.today ,Date.today, Date.today) %>
<table>
<tr>
<ul>
<% #tasks.each do |task| %>
<td>
<li><%= task.check_box :item_due %></li>
</td>
<% end %>
</ul>
</tr>
</table>
I was looking for a way to improve this piece of code:
<% if A || B %>
<a></a>
<ul>
<% if A %>
<li>Action A</li>
<% end %>
<li>Action C</li>
<% if B %>
<li>Action B</li>
<% end %>
</ul>
<% end %>
this involves to evaluate A, eventually B and then A again and B again. Any idea on how to improve that code?
P.S.
Order of actions matters.
In your controller:
#action_list = []
if A || B
#action_list << "Action A" if A
#action_list << "Action C"
#action_list << "Action B" if B
end
In your view:
<% if #action_list.count > 0 %>
<a></a>
<ul>
<% #action_list.each do |action| %>
<li><%= action %></li>
<% end %>
</ul>
<% end %>
I'd go with the same approach as msergeant, but stick it in a class like so:
class ActionListItems
def initialize(a = nil, b = nil)
#a = a
#b = b
end
def render?
!!(#a || #b)
end
def each
yield "Action A" if #a
yield "Action C"
yield "Action B" if #b
end
end
action_list_items = ActionListItems.new "a", "b"
puts action_list_items.render?
#=> true
action_list_items.each do |item|
puts item
end
#=> Action A
#=> Action C
#=> Action B
I am attempting to perform a series of requests that will organize the results in a given date span in a way where each location will live on the top level, and contain an another array with each set of results. Here is what I have so far:
locations = Location.all
#requests = []
locations.each do |location|
request = PurchaseRequest.where('created_at >= ? AND created_at <= ? AND location_id = ?', params[:start_date], params[:end_date], location.id).order(:location_id)
#requests.push(location => request)
end
My ideal (non-working) implementation in the view would look something like:
<ul>
<% #requests.each do |location| %>
<li>location[0].name</li>
<ul>
<% location[1].each do |request| %>
<li>request.name</li>
<% end %>
</ul>
<% end %>
</ul>
Try something like this:
#requests = {}
Location.all.each do |location|
#requests[location.name] = PurchaseRequest.where('created_at >= ? AND created_at <= ? AND location_id = ?', params[:start_date], params[:end_date], location.id).order(:location_id)
end
and:
<ul>
<% #requests.each do |location_name, request_list| %>
<li><%= location_name %></li>
<ul>
<% request_list.each do |request| %>
<li><%= request.name %></li>
<% end %>
</ul>
<% end %>
</ul>
#requests becomes a hash with the key being the location name and the value being the purchase requests.
I have two tables called coordinates and tweets in my database, and I need to perform a search to fetch the tweets from the tweets table. Since this also requires data from coordinates table, I decided to use require and then create an object and then call the function of the other program inside the other program.
My coordinates_controller:
class CoordinatesController<ApplicationController
def paramas(b)
#b = params[:show]
return #b
end
def coor(latitude,longitude)
#latitude=0
#longitude=0
end
def search(min_latitude,max_latitude,min_longitude,max_longitude,sql)
#a = Coordinates.where(city :params[:show] ).take
if(params[:show]== a.city) then
#latitude= a.latitude
#longitude=a.longitude
end
if(#latitude=0 && #longitude=0) then
return #sql="Select * from tweets where tweet_text LIKE '%search%' AND user_loc LIKE 'a.paramas' order by id desc"
else if (#latitude!=0 && #longitude!=0)
#min_lat = #latitude - 1.0
#max_lat = #latitude + 1.0
#min_lng = #longitude - 1.0
#max_lng = #longitude + 1.0
return #sql = "Select * from tweets where tweet_text LIKE '%search%' AND ( ((longitude BETWEEN min_lng and max_lng) AND (latitude BETWEEN min_lat and max_lat)) OR (user_loc LIKE 'a.paramas') ) order by id desc"
else
return #sql="Select * from tweets where tweet_text LIKE '%search%'"
end
end
end
# a= CoordinatesController.new
end
My tweets_controller
require 'coordinates_controller.rb'
#require 'models/coordinates.rb'
class TweetsController<ApplicationController
def show
#include 'coordinates_controller.rb'
a= CoordinatesController.new
#sql=a.search(min_latitude,max_latitude,min_longitude,max_longitude,sql)
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
my view code for the search button
<%= form_tag({controller: "tweets", action:"show" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:show) %>
<%= submit_tag("Search") %>
<% end %>
<%= form_tag({controller: "coordinates", action:"search" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:search) %>
<%= submit_tag("Search") %>
<% end %>
My view code for displaying results
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
I am getting the error undefined method min_latitude. I can't figure out what is causing the error.
When you call your method search Ruby don't know what min_latitude means here
#sql=a.search(min_latitude,max_latitude,min_longitude,max_longitude,sql)