Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我想在RubyonRails中创建新行时将title设为必填字段_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

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