Ruby-on-rails 如何向 button_to 表单添加其他参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4886963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add additional params to a button_to form?
提问by chadoh
I want to have a Submitbutton. It updates one field on the submission; submission.state = :submitted
我想要一个Submit按钮。它更新提交的一个字段;submission.state = :submitted
Now, I could make a custom route and a custom action and just post to that. But that seems really heavy-handed. Especially since I'll also have a rejectbutton and possibly more. Needing a custom route & action for each of those seems downright silly to me.
现在,我可以创建一个自定义路由和一个自定义操作,然后发布到那个。但这似乎真的很严厉。特别是因为我还有一个reject按钮,可能还有更多。对我来说,需要为每一个自定义路由和操作似乎非常愚蠢。
It would be much nicer if I could do something like
如果我能做类似的事情会更好
button_to "Submit", submission_url(submission), :method => :put, :submission => { :state => :submitted }
Which would post to the submission's updatemethod and update only the desired field.
这将发布到提交的update方法并仅更新所需的字段。
But that doesn't work. How can I make it work? Or do you have a better idea of how to do this?
但这不起作用。我怎样才能让它工作?或者你对如何做到这一点有更好的想法?
回答by jayelm
The pull requestmentioned by @AugustinRiedinger has been merged and is now available as of Rails 4.1.0. Now just add the paramsoption:
@AugustinRiedinger 提到的拉取请求已经合并,现在可以从Rails 4.1.0 开始使用。现在只需添加params选项:
params: { state: :submitted }
回答by chadoh
It's not as concise, but without extending Rails, this will get me by:
它不是那么简洁,但没有扩展 Rails,这将使我得到:
= form_for submission, :html => { :class => "button_to" } do |f|
= f.hidden_field :state, :value => :submitted
= f.submit "Submit", :class => "link"
回答by Nicolas Maloeuvre
Add params:{} at the end, it will generate hidden_field
在最后添加params:{},会生成hidden_field
<%= button_to user.name, user, class:"btn btn-default", style:"", method: :patch, remote: true, params: { a_field: false, an_other_field:"a new value" } %>
回答by Joseph
I have something similar that works:
我有类似的东西:
button_to "Submit", submission_url(submission, :submission => { :state => :submitted }), :method => :put
button_to "Submit", submission_url(submission, :submission => { :state => :submitted }), :method => :put
回答by Augustin Riedinger
So, as from this rails pull request : https://github.com/rails/rails/pull/10471
所以,从这个 rails pull request 开始:https: //github.com/rails/rails/pull/10471
Here is what you can do to have your custom button_to.
您可以通过以下方法获得自定义 button_to。
In application_helper.rb, add these lines:
在 中application_helper.rb,添加以下几行:
module ApplicationHelper
// Unfortunately these 2 methods need to be redefined. I don't know how I could access the original ones.
def token_tag(token=nil)
if token != false && protect_against_forgery?
token ||= form_authenticity_token
tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token)
else
''
end
end
def method_tag(method)
tag('input', type: 'hidden', name: '_method', value: method.to_s)
end
def button_to_with_params(name = nil, options = nil, html_options = nil, &block)
html_options, options = options, name if block_given?
options ||= {}
html_options ||= {}
html_options = html_options.stringify_keys
convert_boolean_attributes!(html_options, %w(disabled))
url = options.is_a?(String) ? options : url_for(options)
remote = html_options.delete('remote')
params = html_options.delete('params') { Hash.new }
method = html_options.delete('method').to_s
method_tag = %w{patch put delete}.include?(method) ? method_tag(method) : ''.html_safe
form_method = method == 'get' ? 'get' : 'post'
form_options = html_options.delete('form') || {}
form_options[:class] ||= html_options.delete('form_class') || 'button_to'
form_options.merge!(method: form_method, action: url)
form_options.merge!("data-remote" => "true") if remote
request_token_tag = form_method == 'post' ? token_tag : ''
html_options = convert_options_to_data_attributes(options, html_options)
html_options['type'] = 'submit'
button = if block_given?
content_tag('button', html_options, &block)
else
html_options['value'] = name || url
tag('input', html_options)
end
inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag)
params.each do |name, value|
inner_tags.safe_concat tag(:input, type: "hidden", name: name, value: value.to_param)
end
content_tag('form', content_tag('div', inner_tags), form_options)
end
end
And to use it:
并使用它:
= button_to_with_params 'Awesome button', awesome_action_path, method: :put, :params => {:my_param => 'my_value'}
Enjoy! Have fun Railing!
享受!玩得开心栏杆!
回答by KeiferJ
If I read things correctly what you are effectively wanting to do something specific when a standard rails form is submitted in the standard way.
如果我正确地阅读了内容,那么当以标准方式提交标准 rails 表单时,您实际上想要做一些特定的事情。
Notice that when a form is submitted using e.g.
请注意,当使用 eg 提交表单时
f.submit "Save Changes"
f.submit "Save Changes"
then
然后
params[:commit] = "Save Changes"
params[:commit] = "Save Changes"
The GOOD thing about this is that it can allow you to do some appropriate branching in the controllers update action.
这样做的好处是它可以允许您在控制器更新操作中进行一些适当的分支。
The BAD thing is that it's brittle. If one day you or someone else decides to change the button text, things break.. which is bad.
不好的地方是它很脆。如果有一天您或其他人决定更改按钮文本,事情就会中断……这很糟糕。
K
钾
回答by s01ipsist
As of Rails 3.2.1 you can add additional params to the :html_options hash using the :form key.
从 Rails 3.2.1 开始,您可以使用 :form 键向 :html_options 哈希添加其他参数。
http://apidock.com/rails/v3.2.1/ActionView/Helpers/UrlHelper/button_to
http://apidock.com/rails/v3.2.1/ActionView/Helpers/UrlHelper/button_to
This did not exist prior to 3.2.1 so the more verbose solution of declaring a form with hidden attributes was required.
这在 3.2.1 之前不存在,因此需要声明具有隐藏属性的表单的更详细的解决方案。

