に新しい@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
@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も役に立ちませんでした。
ありがとうございました!
[ネストされたモデルの検証 - エラーは表示されません]の複製が可能です。(http://stackoverflow.com/questions/12972112/nested-model-validation-errors-dont-show) – Vucko
ありがとう@Vucko、それを確認します。 – jbatista
それは重複した質問ではない@Vucko。私は質問を編集してより多くの情報を追加し、重複の可能性を避けました。 – jbatista