Shorter way to render certain text - ruby-on-rails

In a view I would like to render the amount of people living in a person's house.
The follow code works, but I believe there is a shorter and better way to write the code.
<% if current_user.family_size == 1 %>
<li><%= current_user.family_size %> person</li>
<% else %>
<li><%= current_user.family_size %> people</li>
<% end %>
Thanks in advance.

Use pluralize method:
<li><%= pluralize(current_user.family_size, 'person') %></li>
UPDATE:
If there is a need for pluralized noun without a count, use:
'person'.pluralize(2) #=> 'people'

Related

Rails 5 is adding a "." to a link_to path instead of a "/"

I'm trying out Rails 5 and have come across something weird. Everywhere that I've googled says that my code is right. Not sure what I'm doing wrong.
My view code:
<% provide(:title, "View all Users") %>
<% #users = User.all %>
<ul>
<% #users.each do |user| %>
<% #user = user %>
<li><%= link_to user.name, users_path(#user) %><%= user.name %></li>
<% end %>
</ul>
Spits out html:
boop boop
Notice that the path is using a dot instead of a forward slash.
Anyone have any idea why?
I believe the issue is the users_path(#user). You have an extra s there, try user_path(#user) or even better try #user
I would write that line as <li><%= link_to user.name, #user %><%= user.name %></li>
Rails is smart enough to understand that linking an object means you want to go to the show page for the object

Shoppe gem each method on product categories

Just wondering if anyone with any experience with Shoppe know how to call an each method on product categories?
I'm trying to do something like this :
<% Shoppe::ProductCategory.each do |prod| %>
<li><%= prod.name %></li>
<% end %>
but I'm getting an undefined method error. Is there some special method that I'm missing? Shoppe::ProductCategory.all and Shoppe::ProductCategory.first.name both have no problems.
Thanks!
solved
Beautiful, #zoran, beautiful.
Try rewriting it to this instead:
<% Shoppe::ProductCategory.all.each do |prod| %>
<li><%= prod.name %></li>
<% end %>
You need to call all on the Shoppe::ProductCategory to be able to iterate over the collection.
Hope it helps!

Rails - Unique Array in View

Having a Bit of trouble displaying unique results from my database. I have a database called "Activities". Each Activity has an associated Sport through sport_id. There may be many activities with the same sport_id.
I want to display a list of all sports linked to the activities database without displaying (for example "Football") twice.
FYI : Venues have many Facilities and Facilities have many Activities.
Controller:
#sports = Sport.all
#activities = Activity.paginate(page: params[:page])
#facilities = Facility.where(venue_id: #venue.id)
View:
<% #facilities.each do |f| %>
<% #activities.find(:all, :conditions => "facility_id == #{f.id} ").each do |a| %>
<li><%= Sport.find(a.sport_id).name %>, (<%= a.facility_id %>)</li>
<% end %>
<% end %>
This shows:
Football, (2)
Hockey, (2)
Hockey, (2)
Football, (5)
I would like to display just:
Football
Hockey
Any ideas?
A simple solution would be to reduce your array with ruby in the view using: uniq!
<% #facilities.each do |f| %>
<% #activities.find(:all, :conditions => "facility_id == #{f.id} ").uniq! { |a| a.sport_id }.each do |a| %>
<li><%= link_to Sport.find(a.sport_id).name, Sport.find(a.sport_id) %></li>
<% end %>
<% end %>
Another way may be to perform a single query on your DB since Sport what you want to narrow down
In controller:
#sports = Sport.joins(activities: [facility: :venue]).where(facilities: { venue_id: #venue.id }).distinct
In view:
<% #sports.each do |sport| %>
<li><%= link_to sport.name, sport %></li>
<% end %>
I am not sure about your DB schema so I went with what I thought you would have done, but it might needs some tweakings.
I hope I helped you.
try to use reject before each
<% #facilities.reject{your condition here}.each do |f| %>

How to prevent any two elements from being the same in a random draw?

Here is my code: (Thank you MurifoX)
<% random_bullets = Bullet.all %>
<ul>
<% 4.times do %>
<li><%= random_bullets.sample.content %></li>
<% end %>
</ul>
I would like to know if making a condition is possible to prevent any two "bullets" from having the same content on the page. If so, could I get some help on the issue, maybe a nudge in the right direction?
Just remove them from the pool as they are used:
<% 4.times do %>
<li><%= random_bullets.delete_at(rand random_bullets.size).content %></li>
<% end %>

get specific element from ruby block, rails app

When performing a block like:
<% #user.favoured_user.each do |user| %>
<li><%= user.name %></li>
<% end %>
With the favoured_user method returning a limit of 5 users, how would I manipulate the block so that even when there are only 3 users available, I could still return 5 li elements?
I'm guessing a helper would come in to play, and maybe the 'first, second, third, etc.' array methods, but I can't think how to write it.
Any help?
You can try this,
<% 5.times do |i| %>
<li> <%= #user.favoured_user[i].try(:name) %> </li>
<% end %>
You can use in_groups_of
Like:
<% #user.favoured_user.in_groups_of(5).each do |favored_user| %>
<% favored_user.each do |user| %>
<li><%= user.try(:name) %></li>
<% end %>
<% end %>
The first 3 users will come through, and the last two entries will be nil

Resources