Rails simple_form - 隐藏字段 - 创建?
如何以简单的形式获得隐藏字段?
以下代码:
= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个
try this
最短!!!
更短,更干燥,也许更明显。
当然,使用 ruby 1.9 和新的哈希格式,我们可以缩短 3 个字符...
Shortest Yet !!!
Shorter, DRYer and perhaps more obvious.
Of course with ruby 1.9 and the new hash format we can go 3 characters shorter with...
也是一种选择。但请注意,它会跳过为表单生成器定义的任何包装器。
Is also an option. Note, however, that it skips any wrapper defined for your form builder.
正确的方法(如果您不尝试重置hidden_field输入的值)是:
其中
:method
是在对象上调用时产生您想要的值的方法所以按照上面的示例:
示例中使用的代码将重置表单传递的 @movie 的值 (:title)。如果您需要访问电影的值(:标题),请执行以下操作,而不是重置它:
再次仅当您不想重置用户提交的值时才使用我的答案。
我希望这是有道理的。
Correct way (if you are not trying to reset the value of the hidden_field input) is:
Where
:method
is the method that when called on the object results in the value you wantSo following the example above:
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:
Again only use my answer is you do not want to reset the value submitted by the user.
I hope this makes sense.
在我的情况下,上述方法都不能完美地工作,因为它们在我的前端留下了一个空白矩形。对我有用的是 -
<%= 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" %>