简体   繁体   中英

Rails: How do I add an additional parameter to resource-based link_to call?

I have a resource defined in routes.rb like so:

map.resources :users

I like using the cleanest link_to option, which would be:

link_to @user

I'd like to add an additional parameter to this call, though: "view=local"

If I were using the user_path, I'd do this:

link_to user_path(@user, { :view => 'local' })

Is there a way to get the same result without including the user_path function explicitly? Ideally, I'd do something like:

link_to @user, { :view => 'local' }

but that doesn't seem to work.

No, you can't. The only real control you have is the ability to add more params on the link tag. link_to has a third parameter that would let you add things like a title attribute on the HTML tag, but it's not going to modify the href value.

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001597

Go ahead and use the user_path helper. As with most things in Rails, if you want to do the default action, it's going to be simple. You want to do something more complex, so you have to do more work :)

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