Why isn't this simple elsif working in Rails/Ruby? - 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!

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

How would I get all people, that are following my in Ruby

I am using the act_as_follower gem, and I get all the users, that I follow, but now I want all the users, that are following me, how would I do that. I checked and I think something like this
current_user.followers, but I cant get it working. My code for people that I am following is this:
<% #users = User.all %>
<% #users.each do |user| %>
<% unless user == current_user %>
<div class="container people-lists list-following">
<%= image_tag("/assets/" + user.avatar + ".png", alt: "Logo", class: "people-image") %>
<%= user.username %>
<% if user_signed_in? %>
<% unless user == current_user %>
<% if current_user.following?(user) %>
<%= button_to("Un-Follow", user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true, class: 'btn btn-primary button') %>
<% else %>
<%= button_to("Follow", user_follows_path(user.to_param), :remote => true, class: 'btn btn-primary button') %>
<% end %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
I found I the User.all returns all of them, and I can use the all_following, and follows like this and it works.
<% #users = current_user.all_following %>
And like this
<% #users = current_user.followers %>

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

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

What happened to the flash messages in Rails 3.2.1?

I use this code to display flash messages in Rails 3.2.1 apps (well, I did):
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
<%= msg %>
</div>
<% end %>
But I get nothing. So I put <%= debug flash %> to see what I was getting back after an update and I get this:
--- !ruby/object:ActionDispatch::Flash::FlashHash
used: !ruby/object:Set
hash: {}
closed: false
flashes: {}
now:
How to I do the same thing?
The code I use for displaying flash messages and alerts is:
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :class => "flash flash_#{name}" %>
<% end %>
But if your flash hash is empty, it's not going to display anything. Are you sure you're setting your flash hash properly?

Resources