简体   繁体   中英

Form not showing error messages (Rails 3 project)

My submit form is not showing error messages...when I fill it out incorrectly and submit, it just refreshes. It should show error messages - see attached code -- but for some reason it isn't. Not sure why this is happening.

Any advice would be much appreciated!

Thanks,

Faisal

_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 %>

POSTS CONTROLLER

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

POST MODEL

class Post < ActiveRecord::Base
validates :title, :job, :location, :salary, :presence => true 
validates :salary, :numericality => {:greater_than_or_equal_to => 1} 
end

Posts > New.Html.Erb file

<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>

As your form show all errors, there might be another problem prevent saving models. Make sure you can save models in rails console.

Are you using Devise or another authentication library.

You might need to put an authenticity token in your forms (although that would log you out rather just redirect, but it might be the behavior if you don't have an auth library)

You should also use form_for @post so that your inputs get named correctly for your new function (that would put in the auth token for you).

You problem might also be in your model, you might have to whitelist the attributes sent by your form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM