如何在 Formtastic 中为单选按钮集合选择默认值?

发布于 2024-10-08 22:49:39 字数 1086 浏览 5 评论 0原文

我正在 Rails3 和 Formtastic 中构建一个表单。 我有以下字段:

<%= f.input :housing, :as => :radio, :collection => {"Awesome" => "one", "Great" => "two", "Nice" => "three"} %>

生成类似于以下内容的 HTML:

<input id="post_one" name="post" type="radio" value="one" />Awesome</label>
<input id="post_two" name="post" type="radio" value="two" />Great</label>
<input id="post_three" name="post" type="radio" value="three" /> Nice</label>

工作完美!

现在我想知道如何传递一个将“Great”标记为默认(选定)值的选项。我尝试执行以下操作,但无法使其工作。

<%= f.input :housing, :as => :radio, :collection => {"Awesome" => "one", "Great" => "two", "Nice" => "three"}, :default => "one" %>

我还尝试传递 :selected:checked 而不是 :default 但可惜,它不起作用。

有人知道有办法做到这一点吗?

谢谢!


编辑: Aditya 提出了一个非常好的观点。一些搜索产生了这个有用的提示

I'm building a form in Rails3 and Formtastic.
I have the following field:

<%= f.input :housing, :as => :radio, :collection => {"Awesome" => "one", "Great" => "two", "Nice" => "three"} %>

Which generates HTML similar to:

<input id="post_one" name="post" type="radio" value="one" />Awesome</label>
<input id="post_two" name="post" type="radio" value="two" />Great</label>
<input id="post_three" name="post" type="radio" value="three" /> Nice</label>

That work flawlessly!

Now I'd like to know how I could pass in an option that would mark "Great" as the default (selected) value. I tried doing the following, but I can't get it to work.

<%= f.input :housing, :as => :radio, :collection => {"Awesome" => "one", "Great" => "two", "Nice" => "three"}, :default => "one" %>

I also tried passing in :selected and :checked instead of :default but alas, it does not work.

Does anybody know of a way to do this?

Thanks!


Edit: Aditya brings up a very good point. Some searching yielded this helpful tip.

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

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

发布评论

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

评论(3

不必在意 2024-10-15 22:49:39

使用集合成员数组中的第三个元素设置特定单选输入选项上的 HTML 选项,如下所示:

<%= f.input :author, :as => :radio, :collection => [["Test", 'test'], ["Try", "try", {:checked => true}]]

Set the HTML options on a specific radio input option with a 3rd element in the array for a collection member as follows:

<%= f.input :author, :as => :radio, :collection => [["Test", 'test'], ["Try", "try", {:checked => true}]]
瘫痪情歌 2024-10-15 22:49:39

您是否尝试过将模型的 :housing 属性值设置为默认值?您可以在启动表单之前或在控制器中执行此操作,或者最好的方法是在模型初始化中执行此操作?恕我直言,视图可能不是默认的最佳位置。

Have you tried setting the value of the :housing attribute of your model to the default value? You could do that just before you start the form or in the controller or the best way is to do that in the model initialize? View may not be the best place to default IMHO.

掩耳倾听 2024-10-15 22:49:39

视图中不再有执行此操作的选项,正确的方法是默认使用正确的值初始化模型,或者将模型置于控制器中的该状态,如 Aditya 所描述的。

There is no longer an option to do this in the view, the correct way is to initialize the model with the correct value by default, or to put the model into that state in the controller, as described by Aditya.

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