Rails i18n list of items and looping in view -
how can list elements in yml
, loop through them in view , access properties? current code gets last item in list. want loop through in view list of items , display title
, 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 random description second item item description
use instead:
<% t('front_page.index.description_section.items').each |item| %> # ^ no equal sign here <%= item[:title] %> #^^^^ hash <%= item[:description] %> <% end %>
also, items list not defined properly:
t('front_page.index.description_section.items.item.title') # => returns "second item" because key `item` has been overwritten
use following syntax define array in yaml:
items: - title: "first item" description: "a random description" - title: "second item" description: "another item description"
to check this, can in 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
Comments
Post a Comment