0

送信フォームにエラー メッセージが表示されません...間違って入力して送信すると、更新されるだけです。エラーメッセージが表示されるはずです-添付のコードを参照してください-しかし、何らかの理由で表示されません。なぜこれが起こっているのか分かりません。

どんなアドバイスでも大歓迎です!

ありがとう、

ファイサル

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

2 に答える 2

0

フォームにすべてのエラーが表示されるため、モデルの保存を妨げる別の問題がある可能性があります。モデルを Rails コンソールに保存できることを確認してください。

于 2012-04-10T09:27:42.193 に答える
0

Devise または別の認証ライブラリを使用していますか。

フォームに認証トークンを配置する必要がある場合があります (ただし、リダイレクトするだけでなく、ログアウトすることになりますが、認証ライブラリがない場合は動作する可能性があります)。

また、入力が新しい関数に対して正しく命名されるように使用する必要がありますform_for @post(これにより、認証トークンが挿入されます)。

モデルにも問題がある可能性があります。フォームから送信された属性をホワイトリストに登録する必要がある場合があります。

于 2012-04-12T09:32:18.603 に答える