Ruby on rails 如何在当前视图/局部视图上渲染和显示错误?
我有一个表单分部,我想在分部上显示错误消息,而不重定向到其他地方 我该怎么做 这是我的PostsController创建 这是用户所在的简单表单部分,我希望在右侧字段内联显示错误消息,甚至在表单顶部显示错误消息,而无需重定向Ruby on rails 如何在当前视图/局部视图上渲染和显示错误?,ruby-on-rails,validation,ruby-on-rails-4,simple-form,Ruby On Rails,Validation,Ruby On Rails 4,Simple Form,我有一个表单分部,我想在分部上显示错误消息,而不重定向到其他地方 我该怎么做 这是我的PostsController创建 这是用户所在的简单表单部分,我希望在右侧字段内联显示错误消息,甚至在表单顶部显示错误消息,而无需重定向 <%= simple_form_for(@post, html: {class: 'form-horizontal' }) do |f| %> <%= f.error_notification %> <%= f.input_f
<%= simple_form_for(@post, html: {class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<%= f.input_field :parent_id, as: :hidden %>
<div class="row">
<div class="col-xs-12">
<% if can? :manage, @post %>
<label for="status">
Status
</label>
<%= f.input_field :status, label: "Status", collection: Post.statuses.keys, selected: :unconfirmed %>
<% end %>
<% if can? :manage, @post %>
<label for="title">
Title
</label>
<%= f.input_field :title, placeholder: "Enter Title" %>
<span id="wordCountTitle">0</span> words<br/>
<span class="help-block">A block of help text that breaks onto a new line and may extend beyond one line.</span>
<% end %>
<div class="report-field">
<label for="report">
Report
</label>
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-file-text"></span>
</span>
<%= f.input_field :body, id: "body-field", placeholder: "Provide all the facts of what happened, who did it, and where it happened.", class: "form-control", rows: "4" %><br />
</div>
<span class="help-block">Please avoid speculation.</span>
<span id="wordCountBody">0</span> / 150 words
</div>
<br/>
<label for="name">
Check if you are an eye witness:
</label>
<%= f.input_field :has_eyewitness, boolean_style: :inline %>
<br/>
<div>
<label for="photoFileUpload">Upload Images:</label>
<%= f.input_field :photo %>
<p class="help-block">Allowed: png, gif, jpg/jpeg</p>
</div>
<div>
<label for="FileUpload">Upload Documents, Video, or Audio:</label>
<%= f.input_field :file %>
<p class="help-block">Allowed: pdf, doc, xls, mp4, m4v, mp3, wav.</p>
</div>
</div>
<% if can? :manage, @post %>
<%= f.input :youtube_embed_code %> <br />
<%= f.input :soundcloud_embed_code %> <br />
<% end %>
<div class="col-xs-12">
<%= f.button :submit, class: "btn btn-primary pull-left" %>
</div>
</div> <!-- //row -->
<% end %>
现在,只要在创建过程中出现错误,它就会根据控制器呈现posts/new.html.erb视图。render:new不会重定向到新操作,它只呈现新视图。你到底想达到什么目的?@Nitish我想做的是,每当用户试图填写此表单来验证我的验证,例如密码太短、标题空白等。它应该在那里向他们显示错误消息,而不会将其发送到另一个页面,并在新页面上向他们显示错误。换句话说,我希望服务器端验证运行,但填写的表单将与错误一起显示,以便用户可以修复并成功提交表单。我从未使用过简单表单。但通常我用来显示验证错误。@Nitish是否会在不刷新整页的情况下显示在表单上?如果您根本不想刷新页面,则必须使用或类似gem
<%= simple_form_for(@post, html: {class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<%= f.input_field :parent_id, as: :hidden %>
<div class="row">
<div class="col-xs-12">
<% if can? :manage, @post %>
<label for="status">
Status
</label>
<%= f.input_field :status, label: "Status", collection: Post.statuses.keys, selected: :unconfirmed %>
<% end %>
<% if can? :manage, @post %>
<label for="title">
Title
</label>
<%= f.input_field :title, placeholder: "Enter Title" %>
<span id="wordCountTitle">0</span> words<br/>
<span class="help-block">A block of help text that breaks onto a new line and may extend beyond one line.</span>
<% end %>
<div class="report-field">
<label for="report">
Report
</label>
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-file-text"></span>
</span>
<%= f.input_field :body, id: "body-field", placeholder: "Provide all the facts of what happened, who did it, and where it happened.", class: "form-control", rows: "4" %><br />
</div>
<span class="help-block">Please avoid speculation.</span>
<span id="wordCountBody">0</span> / 150 words
</div>
<br/>
<label for="name">
Check if you are an eye witness:
</label>
<%= f.input_field :has_eyewitness, boolean_style: :inline %>
<br/>
<div>
<label for="photoFileUpload">Upload Images:</label>
<%= f.input_field :photo %>
<p class="help-block">Allowed: png, gif, jpg/jpeg</p>
</div>
<div>
<label for="FileUpload">Upload Documents, Video, or Audio:</label>
<%= f.input_field :file %>
<p class="help-block">Allowed: pdf, doc, xls, mp4, m4v, mp3, wav.</p>
</div>
</div>
<% if can? :manage, @post %>
<%= f.input :youtube_embed_code %> <br />
<%= f.input :soundcloud_embed_code %> <br />
<% end %>
<div class="col-xs-12">
<%= f.button :submit, class: "btn btn-primary pull-left" %>
</div>
</div> <!-- //row -->
<% end %>