Rails - SimpleForm 中默认选择的单选按钮:集合

发布于 2024-11-17 04:02:31 字数 235 浏览 3 评论 0原文

我对 SimpleForm 中的单选按钮没什么问题。

当我使用

= f.association :manufactureType, :collection => ManufactureType.all, :as => :radio

Rails 时,只会生成几个单选按钮,但没有一个被选中。 我希望默认选择第一个单选按钮。我怎样才能做到呢?

谢谢

I've litte problem with radiobuttons in SimpleForm.

When i use

= f.association :manufactureType, :collection => ManufactureType.all, :as => :radio

Rails simply generates few radiobuttons, but none of them are selected.
I want first radiobutton to be selected by default. How can i make it?

Thanks

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

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

发布评论

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

评论(4

拔了角的鹿 2024-11-24 04:02:31

如果将制造类型传递到视图中,则可以执行以下操作:

:checked => @manufacture_types[0]

或者

:checked => ManufactureType.first

If you pass in the manufacture types into the view, you can do the following:

:checked => @manufacture_types[0]

Or

:checked => ManufactureType.first
捶死心动 2024-11-24 04:02:31

我的例子稍微复杂一些,其他答案都不适合我,因为没有可供参考的集合或模型。

= f.input :attending, as: :radio_buttons, :collection => [ ['Yes', true], ['No', false] ], :checked => ['Yes', true]

My example was slightly more complicated, none of the other answers worked for me since there was no collection or model to reference.

= f.input :attending, as: :radio_buttons, :collection => [ ['Yes', true], ['No', false] ], :checked => ['Yes', true]
半岛未凉 2024-11-24 04:02:31

根据操作员的评论,添加此参数对我有用:

:checked => 1

from op's comment, adding this parameter worked for me:

:checked => 1
放飞的风筝 2024-11-24 04:02:31

这是我的有效代码的摘录:

= f.input :body_format,
  collection: [['markdown', 'Markdown']],
  label_method: :last,
  value_method: :first,
  as: :radio_buttons,
  checked: 'markdown', # THIS
  required: true

Here is an excerpt of my code which works:

= f.input :body_format,
  collection: [['markdown', 'Markdown']],
  label_method: :last,
  value_method: :first,
  as: :radio_buttons,
  checked: 'markdown', # THIS
  required: true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文