希望 Rails simple_form 单选按钮显示不是值的文本

发布于 2024-12-06 03:23:26 字数 1000 浏览 1 评论 0原文

我正在使用 simple_form gem https://github.com/plataformatec/simple_form 创建一些输入字段;其中之一是单选按钮组,如下所示:

<%= f.input :due_date, :collection => ['Today', 'Tomorrow', 'In 3 Days'], :as => :radio %>

因此数据库“due_date”中的该字段是一个日期。但我们知道,人们通常只想要这三个选项,而不是让人们点击那个小日历。我确实想使用单选按钮。但该行的输出表明,如果我现在点击提交按钮, due_date 参数将具有此处所述的值,即今天、明天或 3 天后。以下是 Today 部分的输出 HTML。

<span>
<input class="radio optional" id="project_due_date_today" name="project[due_date]" type="radio" value="Today">
<label class="collection_radio" for="project_due_date_today">Today</label>
</span>

我想要的理想情况是这样的:

<%= f.input :due_date, :collection_to_params => [Date.today, Date.tomorrow, Date.today+3], :display_value => ['Today', 'Tomorrow', 'In 3 Days'], :as => :radio %>

所以当用户点击一个并提交时,我实际上会得到一些发送到服务器的 ISO 日期。

有什么想法吗?

谢谢!

I am using the simple_form gem https://github.com/plataformatec/simple_form to create some input fields; one of which is a radio button group, like this:

<%= f.input :due_date, :collection => ['Today', 'Tomorrow', 'In 3 Days'], :as => :radio %>

So this field in the database "due_date" is a Date. But rather than letting people click on that smallish calendar, we know that generally people just want these three options. And I do want to use radio button. But the output of this line suggest that if I hit the submit button now, the due_date param will have the values stated there, i.e., Today, Tomorrow or In 3 Days. Here's the output HTML for the Today part.

<span>
<input class="radio optional" id="project_due_date_today" name="project[due_date]" type="radio" value="Today">
<label class="collection_radio" for="project_due_date_today">Today</label>
</span>

What I want ideally is like :

<%= f.input :due_date, :collection_to_params => [Date.today, Date.tomorrow, Date.today+3], :display_value => ['Today', 'Tomorrow', 'In 3 Days'], :as => :radio %>

So when a user clicks on one, and submits, I actually get some ISO date sent to the server.

Any ideas?

Thanks!

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

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

发布评论

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

评论(2

千紇 2024-12-13 03:23:26
<%= f.input :due_date, 
:collection => [[Date.today, 'Today'], [Date.tomorrow, 'Tomorrow'], [Date.today+3, 'In 3 Days']], 
:label_method => :last, 
:value_method => :first, 
:as => :radio_buttons %>

因此,您可以将数组的数组传递给集合哈希,然后可以传递另外两个选项来指定此输入的 VALUE 和此输入的可见标签,它们被正确命名为 label_method 和 value_method。 “方法”这个词是指对相关集合的单个项目调用什么方法来检索标签或值。在数组的数组的情况下, array.first 是我的值, array.last 是我的标签。因此上面的代码

<%= f.input :due_date, 
:collection => [[Date.today, 'Today'], [Date.tomorrow, 'Tomorrow'], [Date.today+3, 'In 3 Days']], 
:label_method => :last, 
:value_method => :first, 
:as => :radio_buttons %>

So you can pass either an array of arrays to the collection hash, and then the there are two more options that can be passed to specify the VALUE of this input and the visible label of this input and they are properly named label_method and value_method. the word method being, what method to call on the individual item of the collection in question to retrieve the label or the value. In the array of array's case, array.first is my value, array.last if my label. Hence the code above

萌化 2024-12-13 03:23:26

这是我从 Nik So 的回答中得到的内容,并应用了所有评论:

<%= f.input :due_date, 
collection: [['Today', Date.today], ['Tomorrow', Date.tomorrow], ['In 3 Days', Date.today+3]],
as: :radio_buttons %>

This is what I've got from Nik So's answer with all comments applied:

<%= f.input :due_date, 
collection: [['Today', Date.today], ['Tomorrow', Date.tomorrow], ['In 3 Days', Date.today+3]],
as: :radio_buttons %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文