Ruby on rails 我想在RubyonRails中创建新行时将title设为必填字段
我想强制设置标题字段以保存新行:-Ruby on rails 我想在RubyonRails中创建新行时将title设为必填字段,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我想强制设置标题字段以保存新行:- <h1> Edit Post</h1> <%= form_for @post do |f| %> <p> <br> <%= f.label :title, autofocus: true, placeholder: "title", :required => true %> <br/> <br> <%= f.text_field :title %
<h1> Edit Post</h1>
<%= form_for @post do |f| %>
<p>
<br> <%= f.label :title, autofocus: true, placeholder: "title", :required => true %> <br/>
<br> <%= f.text_field :title %> <br/>
</p>
<p>
<%= f.label :body %><br/>
<%= f.text_area :body %> <br/>
</p>
<p>
<%= f.submit "enter" %>
</p>
<% end %>
编辑帖子
正确%>
不管怎样,这是节省,请告诉我哪里出了问题 在前端视图上,您应该具有:
<h1> Edit Post</h1>
<%= form_for @post do |f| %>
<p>
<br> <%= f.label :title%> <br/>
<br> <%= f.text_field :title, autofocus: true, placeholder: "title", required: true %> <br/>
</p>
<p>
<%= f.label :body %><br/>
<%= f.text_area :body %> <br/>
</p>
<p>
<%= f.submit "enter" %>
</p>
<% end %>
您的模型中是否定义了状态验证程序?您可以添加模型验证
validates:title,presence:true
#models/post.rb
class Post
validates :title, presence: true
end