Domanda

I have a number of link_to's with the same parameters. I want to DRY these out, is it possible to pass a hash of parameters to link to?

link_to 'User Profile', @user, remote: true, disable_with: 'loading', class: 'btn'

How can I pass parameters in for example a hash to the link_to method? I tried the following in vain

args = {remote: true, disable_with: 'loading', class: 'btn'}
link_to 'User Profile', @user, args
È stato utile?

Soluzione

I'd start by having a look at the documentation.

The method is define as such:

link_to(name = nil, options = nil, html_options = nil, &block)

So, yes. You can.

UPDATE:

As requested, here's an example:

link_args = {remote: true, data: {disable_with: 'loading'}, class: 'btn'}
link_to 'User Profile', @user, link_args

Altri suggerimenti

On link_to helper you have this semantic:

link_to(name, options = {}, html_options = {}, &block)

i am not recommended separate options if you want DRY write application helper in app/helpers/application_helper.rb:

module ApplicationHelper
  def my_link_to(arg1, arg2, arg3)
    link_to('User Profile', @user, arg1, arg2, arg3)
  end
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top