我的提交表单没有显示错误消息...当我填写错误并提交时,它只是刷新。它应该显示错误消息 - 请参阅附加代码 - 但由于某种原因它不是。不知道为什么会这样。
任何建议将不胜感激!
谢谢,
费萨尔
_FORM.HTML.ERB
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
I am a <%= f.text_field :title %> getting married in <%= f.text_field :job %> in <%= f.text_field :location %>, and looking for a wedding photographer. My budget is <%= f.text_field :salary %>.
</div>
<form>
<div id="first_button">
<button type="button" id="your_button" class="btn span6 large">Submit</button>
</div>
<div id="real_form">
<%= recaptcha_tags %>
<button type="submit" class="btn span6 large">Submit</button>
</div>
</form>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#real_form").hide(); //this will hide the `real_form` div on page load
$("#your_button").click(function() {
$("#real_form").show(); // this will show the `real_form` on clicking of `any_button` button
$("#first_button").hide();
});
});
</script>
<% end %>
帖子控制器
def create
@post = Post.new
respond_to do |format|
if verify_recaptcha
if @post.save
format.html { redirect_to :action=> "index"}
format.json { render :json => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.json { render :json => @post.errors, :status => :unprocessable_entity }
end
else
flash[:message] = 'Please try filling out Recaptcha again' #added to confirm an error is present
format.html { render :action => "new" }
format.json { render :json => @post }
end
end
end
后模型
class Post < ActiveRecord::Base
validates :title, :job, :location, :salary, :presence => true
validates :salary, :numericality => {:greater_than_or_equal_to => 1}
end
帖子 > New.Html.Erb 文件
<div class="hero-unit">
<h1>Find wedding photographers with ease.</h1>
<br>
<p>Post your needs and sit back and wait to get responses.</p>
<br>
<%= render 'form' %>
<%= flash[:message] %>
</div>