我有一个表单部分,我想在部分显示错误消息,而不重定向到其他地方。
我该怎么做?
这是我的PostsController#Create
def create
@post = current_user.posts.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Report was successfully sent to our editors for review.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
这是用户所在的simple_form部分,我想在右侧字段内嵌或甚至只是在窗体顶部显示错误消息而不重定向。
<%= 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
视图(根据控制器。