How can I list elements in my yml and loop through them in the view and access their properties? My current code only gets the last item in the list. I want to loop through in the view the list of items and display their title and description elements.
e.g.
yml:
en:
hello: "Hello world"
front_page:
index:
description_section:
title: "MyTitle"
items:
item:
title: "first item"
description: "a random description"
item:
title: "second item"
description: "another item description"
view:
<%= t('front_page.index.description_section.items')do |item| %>
<%= item.title %>
<%= item.description %>
<%end %>
Result:
{:item=>{:title=>"second item", :description=>"another item description"}}
Desired Result:
first item
a random description
second item
another item description
Use this instead:
<% t('front_page.index.description_section.items').each do |item| %>
# ^ no equal sign here
<%= item[:title] %>
#^^^^ this is a hash
<%= item[:description] %>
<% end %>
Also, your items list is not defined properly:
t('front_page.index.description_section.items.item.title')
# => returns "second item" because the key `item` has been overwritten
Use the following syntax to define an array in YAML:
items:
- title: "first item"
description: "a random description"
- title: "second item"
description: "another item description"
To check this, you can do in your IRB console:
h = {:items=>[{:title=>"first item", :description=>"desc1"}, {:title=>"second item", :description=>"desc2"}]}
puts h.to_yaml
# => returns
---
:items:
- :title: first item
:description: desc1
- :title: second item
:description: desc2
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 %>
Given a list of URLs and text, I want to print out links for each of them.
Is this set up correctly? I'm getting some strange errors (e.g. printing out the 2nd element 3 times).
<%
nav_links = [{
text: "hello",
href: "/hello.html"
},
{
text: "bye",
href: "/bye.html"
},
{
text: "whatever",
href: "/whatever.html"
}]
%>
Print the nav links.
<% nav_links.each do |nav_link| %>
<%= link_to nav_link['text'], nav_link['href'] %>
<% end %>
Your keys are symbols, not strings. It should be nav_link[:text] and nav_link[:href]
Your problem is that nav_link['text'] is not the same thing as nav_link[:text].
If you want to use text you need to say nav_link['text'.to_sym]
'text' is a string
:text is a symbol
Is there a way to loop through a specific array?
For example, user.org_names returns:
["NEW", "Gold", "HEALTH SPRING"] ["Text Illinois"] ["Star Gold"] ["NEW", "Star Gold"] ["NEW", "Star Gold", "HEALTH SPRING"] ["Star Gold"] ["Star Gold"] ["Text Illinois", "Star Text", "Star Gold"] ["Text Illinois", "HEALTH SPRING"] ["NEW", "Star Gold", "Star Text"]
Now, I want to loop only through the fourth array ["NEW", "Star Gold"].
Is that a possibility in Ruby? I couldn't find such a feature.
You can use the basic array notation [] with the index as argument to get the value:
user.org_names[3] # get the 4th element of the array returned
# returns nil if does not exists
In your case, if you want to loop on the 4th element:
fourth_element = user.org_names[3]
fourth_element.each do |element|
# use element
end if fourth_element.present?
This behaves the same as:
fourth_element = user.org_names[3]
if fourth_element.present?
fourth_element.each do |element|
# use element
end
end
if after the end
1.9.3p489 :006 > [1,2,3].each do |n|
1.9.3p489 :007 > puts n
1.9.3p489 :008?> end if false
=> nil
Basically the same as doing:
[1,2,3].each{ |n| puts n } if false
But using a do/end syntax and multi-line
You can do that this way
user.org_names[3].each do |element|
puts element
end
If this is an array of arrays, maybe you could get the fourth element (user.org_names[3]) of the array and loop through it like this:
<% i = 0 %>
<% user.org_names[3].each do |a| %>
<% a[i] %>
<% i+=1 %>
<% end %>
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.
I have a simple controller called list_controller with an index that basically does
def index
#lists = List.all
end
Then i have a view called index.html.haml that looks like:
%h1 My lists
= #lists.each do |list|
.row-fluid
%h2= list.title
%p= list.description
%hr
= link_to "New list", new_list_url
This works and renders but at the bottom of my list is what appears to be a ruby print of the lists objects, in this case a nice big ugly:
[#<List id: 1, title: "Petes todo list", description: "This is petes main list, all sorts of good stuff", created_at: "2012-03-26 21:42:57", updated_at: "2012-03-26 21:42:57">, #<List id: 2, title: "Petes work list", description: "A list for petes work stuff", created_at: "2012-03-26 22:09:50", updated_at: "2012-03-26 22:09:50">]
Why is this happening? How can I stop it?
You're outputting the result of the each, change:
= #lists.each do |list|
to:
- #lists.each do |list|
Haml documentation: = vs -
= #lists.each do |list| tells Haml to evaluate Ruby code to the right and then print out the return value. You should rewrite your view to
%h1 My lists
- #lists.each do |list|
.row-fluid
%h2= list.title
%p= list.description
%hr
= link_to "New list", new_list_url