Conditional statement in "each...do" in Ruby? - 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 %>

Related

Middleman shows syntax error when using a link_to block

I'm using Middleman 4.2 with Middleman-blog 4.0.2.
When I have:
<% blog.tags.each do |tag, articles| %>
<%= link_to "#{tag.titleize} - #{articles.size}", tag_path(tag) %>
<% end %>
I get the desired <a> element output:
Test Tag - 1
But when I change the link_to to a block:
<% blog.tags.each do |tag, articles| %>
<%= link_to tag_path(tag) do %>
<%= tag.titleize %> - <%= articles.size %>
<% end %>
<% end %>
I get a syntax error:
/source/blog/index.html.erb:43: syntax error, unexpected ')' ...<< ( link_to tag_path(tag) do ).to_s; #_out_buf << '
I can't seem to figure out why I'm not able to get the same output here.
Any pointers?
I just realized I had the wrong erb tag on the line with the link_to helper.
The correct code looks like this:
<% blog.tags.each do |tag, articles| %>
<% link_to tag_path(tag) do %>
<%= tag.titleize %> - <%= articles.size %>
<% end %>
<% end %>

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

iterate through array of hashes and output key, value

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

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.

Resources