简体   繁体   English

使用link_to选项作为选项的Rails辅助方法

[英]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. 我正在尝试创建一个辅助方法,该方法可以为link_to方法提供可选参数。 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. link_to方法无法识别extra_settings以及引发和错误。

Any ideas? 有任何想法吗? Thanks! 谢谢!

The link_to method accepts only 3 arguments. link_to方法只接受3个参数。 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 . 此外,你会注意到我用h来逃避你的icontext Just to be safe, because you disabled the auto-escaping that is usually done by Rails by using raw . 为了安全起见,因为你禁用了通常由Rails使用raw完成的自动转义。

Don't reinvent the wheel. 不要重新发明轮子。 The CSS class is an option you can pass to the link_to helper via the options hash. CSS类是一个可以通过options hash传递给link_to帮助器的选项。 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. 现在, link_to也接受一个块。 Use it to simplify your code pass the icon ('globe' or 'flag', etc...) as a block. 使用它来简化代码将图标('globe'或'flag'等)作为块传递。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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