Ruby on Rails表单选择 [英] Ruby on Rails form select
问题描述
<%= form_for @message do | f | %GT;
<%= f.text_field:name%>
<%= f.select:topic ['Test1','test2']
<%end%>
现在,此代码适用于在文本框中提交信息,但由于某些原因而不是信息在选择框中。然而,我的主要问题是,有没有办法,我可以做一个选择框和轨道自动包括:请选择一个选项,这样当我做 validates_presence_of:主题
在选择框中,如果没有更改,它将返回false?
此外,您认为您知道为什么选择框未提交信息我的数据库?
提前感谢
你想要很容易做到。只需将一对值(您的占位符文本和空白值)添加到选择选项的开头。尝试这样:
<%= f.select:topic,[[请选择一个选项',nil],'Test1' 'test2']%>
Hey guys I'm a rails newbie and I have a question about select boxes in forms. Here is my code right now.
<%= form_for @message do |f| %>
<%= f.text_field :name %>
<%= f.select :topic ['Test1', 'test2']
<% end %>
Now, this code works for submitting the information in the text boxes, but for some reason not the information in the select box. However my main question is, Is there a way that I can make a select box and rails automatically include: "Please select an option" and that way when I do validates_presence_of :topic
on the select box it will return false if it hasn't been changed?
Also, do any of you think you know why the select box isn't submitting info to my database?
Thanks in advance!
Yes, what you want is easy to do. Just add a pair of values, your placeholder text and a blank value, to the beginning of your select options. Try this:
<%= f.select :topic, [['Please select an option', nil], 'Test1', 'test2'] %>
这篇关于Ruby on Rails表单选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!