How to short this view code - ruby-on-rails

Hi I have included given code in my view
<% if employee.profile.present? %>
<%= employee.profile.name %>
<% end %>
Please guide me how to dry this code

Two ways I would suggest.
<%= employee.profile.try(:name) %>
<%= employee.profile.name if employee.profile %>

An other good solution is to use an helper:
<%= has_profile(employee) %>
def has_profile(employee)
employee.profile.name if employee.profile.present?
end

Related

Ruby on rails - each do class?

<% consents_checkboxes.each do |checkbox| %>
<%= checkbox.html_safe %>
<% end %>
Hello there,
can i give them a class while looping through them? I can't get it to work and tried several different ways.
This is something I would like to achieve
<% consents_checkboxes.each do |checkbox| %>
<%= checkbox.html_safe, class: 'checkbox' %>
<% end %>
thank you
You can only do it with an element. What you want to do is:
<% consents_checkboxes.each do |checkbox| %>
<p class="checkbox"><%= checkbox.html_safe %></p>
<% end %>
Of course, you can use another element (span, div etc.).
What's on consents_checkboxes? You should provide more context when you ask for something...
It looks like you have strings with the html code, right? you will have to parse the string with something like nokogiri and add a class
<%= Nokogiri.parse(checkbox).add_class('checkbox') -%>
Or you could modify the process that generates that consents_checkboxes to include the class you need. Maybe there's better options, but with only that information it's really hard to tell.

How to display the creation date of a badge earning with merit gem

I'm trying to find a way to display in a View the badge & the "granted_at" record. It would render (in the view) :
<% #profil.badges.each do |badge| %>
<%= image_tag (badge.custom_fields[:image]), badge.granted_at %>
<% end %>
Should I enable the MeritObserver to get this on the view ? Or there is a simpler solution?
(I'm on Rails 5)
EDIT
Thanks to TuteC, we have an answer :
<% #profil.sash.badges_sashes.each do |badge_sash| %>
<%= image_tag (badge_sash.badge.custom_fields[:image]) %><%= badge_sash.created_at %>
<% end %>
You've got to go through sash and badges_sashes relations:
<% #profil.sash.badges_sashes.each do |badge_sash| %>
<%= image_tag(badge_sash.badge.custom_fields[:image]), badge_sash.created_at %>
<% end %>
You can read about merit internals in https://github.com/merit-gem/merit/wiki/General-merit-workflow.

printing ActiveRecord::Associations array elements in rails

I'm trying to display a list of elements from an Associations array:
<%= Event.find_by_id(params[:id]).attendees.to_a.map do |att| %>
<%= att.name %>
<% end %>
the output in the browser is the following:
Attendee-name ["\n"]
How can I get rid of the ["\n"] or what is a better way of manipulating the Associations arrays?
Try this out, this should be what you are going for.
<% Event.find_by_id(params[:id]).attendees.each do |att| %>
<%= att.name %>
<% end %>
Also .chomp is what you need to remove \n but try the above code first.
It was a very stupid problem, this is the solution if anyone gets stuck on the same thing:
<% Event.find_by_id(params[:id]).attendees.to_a.map do |att| %>
<%= att.name %>
<% end %>
leaving the <%= on the first line was the cause of the problem.

Logic to an if statement

I have the following logic in my view to choose which avatar picture to show depending on whether a persons profile is present
<% if #profile %>
<%= image_tag(#profile.avatar_url(:thumb)) %>
<% else %>
<%= image_tag(default_image_url) %>
<% end %>
Helper method
def default_image_url
hash = Digest::MD5.hexdigest(current_user.email)
"https://secure.gravatar.com/avatar/#{hash}?s=100&d=mm"
end
This works fine when someone has not created a profile, but when they do and still want to use their Gravatar this logic fails as my if condition then needs to be if
<% if #profile.avatar? %>
<%= image_tag(#profile.avatar_url(:thumb)) %>
<% else %>
<%= image_tag(default_image_url) %>
<% end %>
At the moment when a profile is created with no image uploaded by the user, there is no image displayed at all.
How can I cover all scenarios?
Edit
I'm in the process of trying
<% unless #profile || #profile.avatar %>
A bit of refactoring starting from #ArieShaw's answer:
Helper
def profile_image_url
#profile.try(:avatar?) ? #profile.avatar_url(:thumb) : default_image_url
end
View
<%= image_tag profile_image_url %>
You may use Object#try:
<% if #profile.try(:avatar?) %>
<%= image_tag(#profile.avatar_url(:thumb)) %>
<% else %>
<%= image_tag(default_image_url) %>
<% end %>

Why does the usage of form_tag and form_for change in rails(erb)?

Old usage:
<% form_tag %>
...
<% end %>
<% form_for %>
....
<% end %>
New usage:
<%= form_tag %>
...
<% end %>
<%= form_for %>
....
<% end %>
(I was learning Head First Rails and know the differences in this list)
I knew that scriptlet don't need = in the <>, but if <%= form_tag %> is not a scriptlet, why does it need a <% end %>?
Does anyone have ideas about this?
Firstly, I've got no idea what a 'scriplet' is; anyway, this was a change in rails 3.0 - see the release notes: http://guides.rubyonrails.org/3_0_release_notes.html#helpers-with-blocks.
Rails 3 brings overall consistency in the API and in this case in the view API.
Rule is:
(want_to_display?) ? (use =) : (don't use =)

Resources