简体   繁体   中英

Passing hash of parameters to link_to rails

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

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

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

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