2016-09-26 6 views
0

私のコンソールでは、私が作ったerorr_messages部分がレンダリングされているのを見ることができ、コメントが検証をパスしないと、それはポストされませんが、実際のエラー内容をレンダリングします。Rails:エラーメッセージ部分的レンダリングしない

部分的なエラー:

<% if object.errors.any? %> 
<div id="error_explanation"> 
    <div class="alert alert-danger"> 
     The form contains <%= pluralize(object.errors.count, "error") %> 
    </div> 
    <ul> 
    <% object.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
<% end %> 

コメントフォーム

<%= form_for @comment, url: comments_path do |f| %> 
<%= render 'shared/error_messages', object: f.object %> 
<%= f.hidden_field :user_id, value: current_user.id %> 
<%= f.hidden_field :post_id, value: post.id %> 
<%= f.text_area :content, size: "60x2", placeholder: "Comment on this post..." %> 
<%= f.submit "Comment" %> 

ポストフォーム

<%= form_for [@user, @post] do |f| %> 
<%= render 'shared/error_messages', object: f.object %> 
<%= f.text_area :content, size: "60x12", placeholder: "What do you want to say?" %> 
<%= f.submit "Post" %> 
人の

ユーザ/保存が失敗したときではなくリダイレ​​クトするよりも、あなたのCommentsController#create

<% if @user == current_user %> 
<h4>Welcome <%= current_user.email %>! </h4> 
<%= render "notifications" %> 
<%= render 'shared/post_form' %> 
<%= render 'feed' %> 
<% end %> 

class CommentsController < ApplicationController 
def index 
    @comments = Comment.all 
end 

def new 
    @comment = Comment.new 
    @user = User.find(params[:user_id]) 
end 

def create 
    @user = current_user 
    @comment = @user.comments.build(comment_params) 

    if @comment.save 
     flash[:success] = "Comment Posted!" 
     redirect_back(fallback_location: root_path) 
    else 
     flash[:danger] = "Could not post comment" 
     redirect_back(fallback_location: root_path) 
    end 
end 


private 

def comment_params 
    params.require(:comment).permit(:content, :user_id, :post_id) 
end 
end 

class PostsController < ApplicationController 

def index 
    @posts = Post.all 
    @user = User.find(params[:user_id]) 
    @comment = Comment.new 
end 

def new 
    @post = Post.new 
    @user = User.find(params[:user_id]) 
end 

def create 
    @post = current_user.posts.build(post_params) 
    if @post.save 
     flash[:success] = "Posted!" 
     redirect_to user_path(current_user) 
    else 
     flash[:danger] = "Post could not be submitted" 
     redirect_to users_path 
    end 
end 

private 

def post_params 
    params.require(:post).permit(:content) 
end 
end 
+0

あなたのpartialのファイル名を確認しましたか?それは '_error_messages'で' app/views/shared'にあるはずですか? – araratan

+0

はい、私はそれに名前を付けました。そしてそれはその場所にもあります。 –

答えて

1

を示しています。

redirect_back(fallback_location: root_path) 

が同じページに滞在し、ちょうど「新しい」テンプレートをレンダリングしてみてください。あなたがリダイレクトした場合

render action: "new" 

を、ブラウザが2番目の要求と@commentが新たに構築されたコメントで上書きされますようになります。

同じページにいて、新しいテンプレートをレンダリングすると、すでにロードされていて保存に失敗したインスタンス(このインスタンスにはすべての検証エラーが設定されています)が使用されます(@comment)。

P.S.フラッシュメッセージは、flashのためのものです - あなたのセッションにメッセージを保存し、リダイレクトを越えて生き残ることができる方法です。

+0

唯一の問題は、私がちょうど_comments_form.htmlを使用しているのでnew.html.erb用のテンプレートがないことです。部分erb –

+0

ああ、つまらない!その場合、最も簡単なオプションは、バリデーションエラーメッセージをフラッシュに書き込むことです: 'flash [:danger] ="コメントを投稿できませんでした:#{@@comcom.errors.full_messages} "。より複雑なオプションは[jquery_ujs](https://github.com/rails/jquery-ujs/wiki)と[rails 'data:remote forms](http://guides.rubyonrails.org/working_with_javascript_in_rails)を使用することです。 html) – gmcnaughton

+0

岩の上の偉大なアイデアは、フラッシュに入れて!ご協力いただきありがとうございます! –

関連する問題