我可以在我的控制台中看到我所做的部分erorr_messages正在渲染,如果注释没有通过验证,那么它将不会被发布,但是我无法获得实际的错误内容来呈现。
错误部分:
<% 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" %>用户/节目
<% 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
I can see in my console that the erorr_messages partial I made is getting rendered, and if a comment does not pass validations, then it will not be posted, but I can't get the actual error contents to render.
Error Partial:
<% 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 %>Comment form
<%= 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" %>Post Form
<%= 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" %>Users/show
<% 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
最满意答案
在您的CommentsController#create ,当保存失败时,而不是重定向:
redirect_back(fallback_location: root_path)尝试停留在同一页面上,只渲染“新”模板:
render action: "new"如果您重定向,浏览器将发出第二个请求,并且@comment将被新建的Comment覆盖。
如果您保持在同一页面并呈现“新”模板,它将使用已加载且未能保存的@comment实例(此实例上设置了所有验证错误)。
PS闪存消息可以正常工作,因为这就是flash的用途 - 一种在会话中存储消息的方法,以便它们能够在重定向中存活。
In your CommentsController#create, when the save fails, rather than redirecting:
redirect_back(fallback_location: root_path)Try staying on the same page and just rendering the "new" template:
render action: "new"If you redirect, the browser will make a second request and @comment will get overwritten with a freshly-built Comment.
If you stay on the same page and render the "new" template, it will use the @comment instance that's already loaded and which failed to save (this instance has all the validation errors set on it).
P.S. the flash message works because that's what flash is for - a way to store messages in your session so that they survive across redirects.
更多推荐
发布评论