Rails simple_form - 隐藏字段 - 创建?

发布于 2024-10-25 16:39:55 字数 262 浏览 2 评论 0原文

如何以简单的形式获得隐藏字段?

以下代码:

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

导致此错误:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情绪操控生活 2024-11-01 16:39:55

试试这个

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

try this

= f.input :title, :as => :hidden, :input_html => { :value => "some value" }
音盲 2024-11-01 16:39:55

最短!!!

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

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

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

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

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...

=f.hidden_field :title, value: "some value"
梦行七里 2024-11-01 16:39:55
= f.input_field :title, as: :hidden, value: "some value"

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

= 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.

苏别ゝ 2024-11-01 16:39:55

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

f.hidden_field :method, :value => value_of_the_hidden_field_as_it_comes_through_in_your_form

其中:method是在对象上调用时产生您想要的值的方法

所以按照上面的示例:

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

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

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

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

我希望这是有道理的。

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

f.hidden_field :method, :value => value_of_the_hidden_field_as_it_comes_through_in_your_form

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

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:

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

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

I hope this makes sense.

蓝天 2024-11-01 16:39:55

在我的情况下,上述方法都不能完美地工作,因为它们在我的前端留下了一个空白矩形。对我有用的是 -

<%= f.text_field :title, value: "some value", type: "hidden" %>;

None of the above worked perfectly in my case, as they were leaving a blank rectangle on my frontend. What worked for me was -

<%= f.text_field :title, value: "some value", type: "hidden" %>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文