简体   繁体   中英

Is there a more efficient/proper way to write this in Ruby?

I come from php and am just starting to cut my teeth in ruby and rails.

Is there more efficient or a more 'ruby' way of writing the following?

<% if !@user.twitter_url.empty? %>
    <a class="twitter" href="<%= @user.twitter_url %>">Twitter</a>
<% end %>
<% if !@user.facebook_url.empty? %>
    <a class="facebook" href="<%= @user.facebook_url %>">Facebook</a>
<% end %>
<% if !@user.google_plus_url.empty? %>
    <a class="googleplus" href="<%= @user.google_plus_url %>">Google Plus</a>
<% end %>
<% if !@user.linked_in_url.empty? %>
    <a class="linkedin" href="<%= @user.google_plus_url %>">Linked In</a>
<% end %>

The best way should be to use some Presenter or maybe a helper method. Anyway using metaprogramming you can do

<% [:twitter, :facebook, :google_plus, :linked_in].each do |social| %>
  <% unless @user.send("#{social}_url").blank? %>
    <a class="<%= social %>" href="<%= @user.send("#{social}_url") %>">social.to_s.titleize </a>
  <% end %>
<% end %>

or

<% [:twitter, :facebook, :google_plus, :linked_in].each do |social| %>
  <%= link_to(social.to_s.titleize , @user.send("#{social}_url"), class: social) unless @user.send("#{social}_url").blank? %>
<% end %>

Or even:

<% [:twitter, :facebook, :google_plus, :linked_in].each do |social| %>
  <%= link_to_unless @user.send("#{social}_url").blank?, social.to_s.titleize , @user.send("#{social}_url"), class: social %>
<% end %>

More compact way :

<%= link_to "Twitter", @user.twitter_url if !@user.twitter_url.blank?  %>
<%= link_to "Facebook", @user.facebook_url if !@user.facebook_url.blank?  %>
<%= link_to "Google Plus", @user.google_plus_url if !@user.google_plus_url.blank?  %>
<%= link_to "Linked In", @user.linked_in_url if !@user.linked_in_url.blank?  %>

With present? instead, thanks to Mark Thomas comment :

<%= link_to "Twitter", @user.twitter_url if @user.twitter_url.present?  %>
<%= link_to "Facebook", @user.facebook_url if !@user.facebook_url.present?  %>
<%= link_to "Google Plus", @user.google_plus_url if !@user.google_plus_url.present?  %>
<%= link_to "Linked In", @user.linked_in_url if !@user.linked_in_url.present?  %>

Or you could use unless instead of if ! with blank? , but I generally find code using unless hard to read, bu that's just me!

Use blank? instead of empty? here in case the url fields are nil. While nil.blank? is defined, nil.empty? is not.

<% unless @user.twitter_url.blank? %>
    <%= link_to "Twitter", @user.twitter_url, class: "twitter" %>
<% end %>
<% unless @user.facebook_url.blank? %>
    <%= link_to "Facebook", @user.facebook_url, class: "facebook" %>
<% end %>
<% unless @user.google_plus_url.blank? %>
    <%= link_to "Google Plus", @user.google_plus_url, class: "googleplus" %>
<% end %>
<% unless @user.linked_in_url.blank? %>
    <%= link_to "Linked In", @user.linked_in_url, class: "linkedin" %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM