iterate through array of hashes and output key, value - ruby

I'd like to do the following:
<% tags=[{"2":"birds"},{"3":"cars"}] %>
<% tags.each do |key, value| %>
<%=key %> <%=value %>
<% end %>
but this doesn't work. How would I do this (if possible)?

Since each hash can have any number of key/value pairs:
<% tags=[{"2":"birds"},{"3":"cars"}] %>
<% tags.each do |t| %>
<% t.each do |key,value| %>
<%=key %> <%=value %>
<% end %>
<% end %>

Related

Tell <%= ... %> not to add a space

I use the following code in a template to render a date:
<% if job.end_month do %>
<%= "#{job.end_month}/" %>
<% end %>
<%= "#{job.end_year}" %>
This results in 3/ 2014 and not in 3/2014. How can I force Phoenix not to add a space after "#{job.end_month}/"?
One solution would be to create an external helper that did this for you:
e.g.
def job_string(%Job{end_month: nil}), do: job.end_year
def job_string(job), do: "#{job.end_month}/#{job.end_year}"
Then just use that in your view:
<%= job_string(job) %>
Alternatively if you don't want to do all that and would prefer a messier sort of look:
<% if job.end_month do %>
<%= "#{job.end_month}/#{job.end_year}" %>
<% else %>
<%= "#{job.end_year}" %>
<% end %>

Why isn't this simple elsif working in Rails/Ruby?

If there is a flash notice > "Flash notice"
If there isn't a flash notice and you're on the homepage > "Welcome"
Code:
<% if flash %>
<% flash.each do |name, msg| %>
<%= content_tag :span, msg, id: "flash_#{name}" %>
<% end %>
<% elsif current_page('/') %>
<% print 'Hi' %>
<% end %>
It prints the flashes correctly but not the welcome on the home page. Doesn't seem to matter if I try current_page or root_url or print 'Welcome' or just plain "Welcome" with no code wrapper. Why?
Yes, almost, I wasn't checking the flash properly at the beginning of the block. This works now:
<% if flash.present? %>
<% flash.each do |name, msg| %>
<%= content_tag :span, msg, id: "flash_#{name}" %>
<% end %>
<% elsif current_page?('/') %>
Welcome to The Spain Report.
<% end %>
Thank you all!

Rails 4 has_many relation group by

Guys how I can fixed this issue
I need to get the result of the relation and group the result using the user_id
My main loop something like that
<% if #users.count >0 %>
<% #users.each do |user| %>
<% if user.books.present? %>
<% user.books.each do |book| %> <!----this is the query I want to change---------->
<% if book.user.present? %>
id | <%= book.user.id %>
user | <%= book.user.user_name %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
I'm tried to used a lot of statements like
<% user.books.group_by(&:user_id).each do |book| %>
<% user.books.group_by(&:user_id).uniq.each do |book| %>
<% user.books.group_by(&:user_id).distinct().each do |book| %>
<% user.books.group_by{|us| us.user_id}.each do |book , us| %>
instead of the statement
<% user.books.each do |book|%>
but usually I get issues , so how i can group by array or DB relation in one statement ?
If the issue is that you are experiencing "nil" call errors when you reference user.books.each, you might do this:
<% user.try(:books).each do |book| %>
This allows you to capture the error rather than fall through to the default error handler.
Otherwise, supposing that you have created a has_many and belongs_to relationship, you definitely ought to be using:
user.books.each {|b| }
This will give you books grouped by users.

Ruby variable each loop substitution

I need to replace X_VARIABLE in 2 places.
For the first X_VARIABLE I want to keep the text 'remove'
For the second X_VARIABLE I want to keep 'd_cars_path'
<% #cars.each do |x| %>
<% #a = #b.send(x) %>
<% if #a == true %>
<%= button_to "removeX_VARIABLE", X_VARIABLEd_cars_path(:id => #user.id), class: "btn btn-large btn-primary" %>
<% end %>
<% end %>
I am looking for some help with the variable substitution syntax. Thanks.
I'd write:
<% #cars.each do |x| %>
<% if #b.send(x) %>
<%= button_to "remove#{x}",
send(:"#{x}d_cars_path", id: #user.id),
class: "btn btn-large btn-primary" %>
<% end %>
<% end %>

Conditional statement in "each...do" in Ruby?

In my erb file, I have the following code in the body tag:
<% #tasks.each do |task| %>
<%= task.name %>
<% end %>
This is working, but I only want to display task.name if task.otherAttribute does not equal -1.
For some reason, I can't figure out how to do this! Any tips much would be much appreciated.
Thank you in advance.
Try this:
<% #tasks.each do |task| %>
<%= task.name if task.otherAttribute != 1 %>
<% end %>
Or:
<% #tasks.each do |task| %>
<%= task.name unless task.otherAttribute == 1 %>
<% end %>
I will provide some more options for future reference:
<% #tasks.each do |task| %>
<% if task.otherAttribute != 1 %>
<%= task.name %>
<% end %>
<% end %>
<% #tasks.each do |task| %>
<%= task.otherAttribute == 1 ? '' : task.name %>
<% end %>
Good luck!
I tend to use #select and #reject for this idiom, since that's basically what you're doing.
<%= #tasks.reject{|t| t.other_attribute == -1}.each do |task| %>
<%= task.name %>
<% end %>
These come from the Enumerable module, which most things with an #each method include.
You can put conditionals into your ERB code.
<%= task.name if task.otherAttribute != 1 %>
You can also perform more complex tasks with a more verbose syntax. It's not necessary in your case, but you could also do more traditional if/else blocks like so:
<% if task.otherAttribute != 1 %>
<%= task.name %>
<% end %>

Resources