2016-04-14 10 views
1

に新しい@commentの検証エラーを表示するには、comments#createでコメントを保持できません。リダイレクトビューでモデルエラーを表示するために別のアクションにリダイレクトするときにモデルを渡す方法

TLDR、チュートリアルのrails 4.2.6とruby 2.2.3を使用してブログを作成しました。


ブログは多くの記事を持っており、それぞれの記事は、コメント多くを持っています。

このチュートリアルの後、新しいコメントを投稿するフォームはarticles\view.html.erbにあり、それはcomments#createに残っています。新しいコメントを投稿すると、再度articles#showにリダイレクトされます。 記事、そのコメントを表示し、新規コメントにarticles_controller

def show 
    @article = Article.find(params[:id]) 
end 

<p><strong>Title:</strong><%= @article.title %></ 
<p><strong>Text:</strong><%= @article.text %></p> 

<h2>Comments</h2> 
<% @article.comments.each do |comment| %> 
    <p><strong>Commenter:</strong><%= comment.commenter %></p> 
    <p><strong>Comment:</strong><%= comment.body %></p> 
<% end %> 

<h2>Add a comment:</h2> 
<%= form_for([@article, @article.comments.build]) do |f| %> 
    <p><%= f.label :commenter %><br><%= f.text_field :commenter %></p> 
    <p><%= f.label :body %><br><%= f.text_area :body %></p> 
    <p><%= f.submit %></p> 
<% end %> 

<%= link_to 'Edit', edit_article_path(@article) %> | 
<%= link_to 'Back', articles_path %> 

を投稿する - articles/show.html.erb - 私は同じビューを使用

class Article < ActiveRecord::Base 
    has_many :comments 
    validates :title, presence: true, length: { minimum: 5 } 
end 

class Comment < ActiveRecord::Base 
    belongs_to :article 
end 

および私の問題は、スタート地点予想通りそれは働いた:)


ここ

def create 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.create(comment_params) 
    redirect_to article_path(@article) 
end 

新しいコメントを投稿するです...

私は追加することによって、さらに行くことにしましたコメントモデル

class Comment < ActiveRecord::Base 
    belongs_to :article 
    validates :commenter, presence: true 
    validates :body, presence: true 
end 

を入力し、保存に失敗したときにコメントのエラーを表示します。

まず、私はエラーがその後、私はそれがあまりにも動作しませんでしたarticles_controller

にグローバルVAR @comment
def show 
    @article = Article.find(params[:id]) 
    @comment = @article.comments.build 
end 

<% if @comment.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@comment.errors.count, "error") %> prohibited 
     this comment from being saved:</h2> 
    <ul><% @comment.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %></ul> 
    </div> 
<% end %> 

を試みた記事モデル

<% if @article.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2> 
    <ul><% @article.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %></ul> 
    </div> 
<% end %> 

トラフ表示しようとしました。

限り、私はそれは私が記事のshowアクションredirect_to article_path(@article)にリダイレクト私は新しいコメントを提出するとき以来、そのようには動作しません理解して - のいずれか、それが成功したか、新しいコメントを保存しない場合 - ので、新しいコメントをインスタンスが作成され、前のインスタンスをエラーで上書きします。

私の最善の解決策は、エラーメッセージと共にflashプロパティを使用していました。これは、この

comments_controller

def create 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.create(comments_params) 
    redirect_to article_path(@article), flash: {new_article_errors: @comments.errors.full_messages} 
end 

articles/show.html.erb

<% unless flash[:new_comment_errors].nil? %> 
    <div id="error_explanation"> 
    <h2> 
     <%= pluralize(flash[:new_comment_errors].count, "error") %> prohibited this comment from begin saved: 
    </h2> 
    <ul> 
     <% flash[:new_comment_errors].each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
    </ul> 
    </div> 
<% end %> 

のように見えます。しかし、これはあまりにも最善の解決策ではありません。エラーは表示されますが、すでに入力されている値は失われます(フォームはクリアです)。

@commentをredirect_toに渡す必要がありますか?この

comments_controller

def create 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.build(comments_params) 

    if @comment.save 
    redirect_to article_path(@article) 
    else 
    redirect_to article_path(:comment => params[:comment]) 
    end 
end 

article_controller

def show 
    @article = Article.find(params[:id]) 
    @comment = @article.comments.build 

    @comment.valid? if params[:comment] 
end 

よう何かが今、私はNo route matches、エラーを取得しています。 params[:comment]を受け取る新しいアクションを作成する必要がありますか?それを受け取るためにshowルートを編集するか?

私はすでにNested model validation - errors don't showを読みますが、彼らはcomments_controller新しいアクションを作成することを提案し、私は(新しい投稿フォームarticle/show.html.erbにする必要があります)ことをしたくないので、助けにはなりませんでした。

Nested Model Form Part 1およびNested Model Form Part 2も役に立ちませんでした。

ありがとうございました!

+0

[ネストされたモデルの検証 - エラーは表示されません]の複製が可能です。(http://stackoverflow.com/questions/12972112/nested-model-validation-errors-dont-show) – Vucko

+0

ありがとう@Vucko、それを確認します。 – jbatista

+0

それは重複した質問ではない@Vucko。私は質問を編集してより多くの情報を追加し、重複の可能性を避けました。 – jbatista

答えて

1

ArticlesController#showメソッドにリダイレクトする代わりに、すべてのインスタンス変数が失われるため、表示ビューをレンダリングできます。

if @comment.save 
    redirect_to article_path(@article) 
else 
    render :template => 'articles/show' 
end 

あなたは既に@template@commentインスタンス変数を持っているので、それらは、レンダリングに使用されます。

+0

私は前にこのようなものを探していましたが、見つけられませんでした。私はそれを試してみます。ありがとうございました@SteveTurczyn = b – jbatista

+1

私はタイプミスを入力しました。修正されましたので、うまくいかない場合は、私のサンプルコードをもう一度チェックしてください! – SteveTurczyn

関連する問題