希望 Rails simple_form 单选按钮显示不是值的文本
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,您可以将数组的数组传递给集合哈希,然后可以传递另外两个选项来指定此输入的 VALUE 和此输入的可见标签,它们被正确命名为 label_method 和 value_method。 “方法”这个词是指对相关集合的单个项目调用什么方法来检索标签或值。在数组的数组的情况下, array.first 是我的值, array.last 是我的标签。因此上面的代码
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
这是我从
Nik So
的回答中得到的内容,并应用了所有评论:This is what I've got from
Nik So
's answer with all comments applied: