Ruby-on-rails rails simple_form - 隐藏字段 - 创建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5371480/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:43:25  来源:igfitidea点击:

rails simple_form - hidden field - create?

ruby-on-railsruby-on-rails-3form-forhidden-fieldsimple-form

提问by Linus Oleander

How can you have a hidden field with simple form?

你怎么能有一个简单形式的隐藏字段?

The following code:

以下代码:

= simple_form_for @movie do |f|
  = f.hidden :title, "some value"
  = f.button :submit

results in this error:

导致此错误:

undefined method `hidden' for #SimpleForm::FormBuilder:0x000001042b7cd0

回答by fl00r

try this

尝试这个

= f.input :title, :as => :hidden, :input_html => { :value => "some value" }

回答by Michael Durrant

Shortest Yet !!!

最短的!!!

=f.hidden_field :title, :value => "some value"

Shorter, DRYer and perhaps more obvious.

更短,更干燥,也许更明显。

Of course with ruby 1.9 and the new hash format we can go 3 characters shorter with...

当然,使用 ruby​​ 1.9 和新的哈希格式,我们可以缩短 3 个字符...

=f.hidden_field :title, value: "some value"

回答by Fuad Saud

= f.input_field :title, as: :hidden, value: "some value"

Is also an option. Note, however, that it skips any wrapper defined for your form builder.

也是一种选择。但是请注意,它会跳过为您的表单构建器定义的任何包装器。

回答by Uzzar

Correct way (if you are not trying to reset the value of the hidden_field input) is:

正确的方法(如果您不尝试重置 hidden_​​field 输入的值)是:

f.hidden_field :method, :value => value_of_the_hidden_field_as_it_comes_through_in_your_form

Where :methodis the method that when called on the object results in the value you want

:method对象上调用时会产生您想要的值的方法在哪里

So following the example above:

所以按照上面的例子:

= simple_form_for @movie do |f|
  = f.hidden :title, "some value"
  = f.button :submit

The code used in the example will reset the value (:title) of @movie being passed by the form. If you need to access the value (:title) of a movie, instead of resetting it, do this:

示例中使用的代码将重置表单传递的@movie 的值 (:title)。如果您需要访问电影的值 (:title),而不是重置它,请执行以下操作:

= simple_form_for @movie do |f|
  = f.hidden :title, :value => params[:movie][:title]
  = f.button :submit

Againonly use my answer is you do not want to reset the value submitted by the user.

再次仅使用我的答案是您不想重置用户提交的值。

I hope this makes sense.

我希望这是有道理的。