简体   繁体   中英

Rails helper method with link_to options as optionals

I'm trying to create a helper method that can have optional arguments for link_to method. My intention is to create a helper method for different cases:

# Typical case #1 (can have any number of extra arguments)
<%= toolbar_item('google.com', 'Google', 'globe', 'btn-primary', target: '_blank') %>

# Typical case #2 (extra arguments are optional)
<%= toolbar_item(root_path, 'Start', 'flag', 'btn-primary') %>

Follows the code:

def toolbar_item(url,text,icon,custom_class, optional_extra_settings = {})
  link_to raw("<i class='fa fa-#{icon}'></i> #{text}"), url, class: custom_class, optional_extra_settings
end

It's no good. link_to method does not recognize the extra_settings and raises and error.

Any ideas? Thanks!

The link_to method accepts only 3 arguments. The last argument needs to be a hash. Therefore you have to merge your class setting with the optional extra settings hash.

Change your example to:

def toolbar_item(url, text, icon, custom_class, optional_extra_settings = {})
  html_options = { class: custom_class }.merge(optional_extra_settings)
  link_to raw("<i class='fa fa-#{h icon}'></i> #{h text}"), url, html_options
end

Furthermore, you will notice that I used h to escape your icon and text . Just to be safe, because you disabled the auto-escaping that is usually done by Rails by using raw .

Don't reinvent the wheel. The CSS class is an option you can pass to the link_to helper via the options hash. Let's move it to the options hash and remove one needless argument.

# notice class: 'btn-primary' vs 'btn-primary'
<%= toolbar_item(..., class: 'btn-primary', target: '_blank') %>

Now, link_to also accepts a block. Use it to simplify your code pass the icon ('globe' or 'flag', etc...) as a block.

def toolbar_item(url, text, options = {}, &block)
  if block_given?
    link_to url, options do
      yield
    end
  else
    link_to text, url, options
  end
end

Now, each time you use the helper with an icon, you can specify the icon you want:

<%= toolbar_item 'google.com', class: 'btn-primary' do %>
  <%= content_tag :i, class: 'globe' %> Google
<% end %>

Which begs the question. Do you really need a helper after all? All we did was create a wrapper. You can just do:

<%= link_to 'Google', 'google.com', class: 'btn-primary' %>

<%= link_to 'Google', class: 'btn-primary' do %>
  <%= content_tag :i, class: 'globe' %> Google
<% 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