如何呈现部分表单的验证错误 - Rails

How to render validation errors of form partial - Rails

我正在构建自定义博客。我有 postscomments.

我通过对个别帖子的展示动作的部分呈现评论。

帖子管理员

 class Blog::PostsController < Blog::BaseController

  def show
    @post = Post.find_by_permalink(params[:id])
    @comment = Comment.new(:post => @post)
  end

end

评论控制器

class Blog::CommentsController < Blog::BaseController

  def create
    @comment = Comment.new(comment_params)
    if @comment.save
      flash[:success] = "Comment successfully created!"
      redirect_to blog_post_url(@comment.post)
    else
      flash[:warning] = "Something went wrong, try again. If problem persists please let our team know about it!"
      redirect_to :back
    end
  end

  private
      def comment_params
        params.require(:comment).permit(:body,:name,:email,:user_id,:post_id)
      end
end

show.html.erb

<div class="row post-container">
  <div class="large-offset-1 large-7 medium-12 columns post-content">
    <h1 class="post-title"> <%= link_to @post.title, blog_post_path(@post) %> </h1>
    <p class="published-date"><em>Published on <%= l @post.published_at, format: :date %></em></p>

    <div class="post-body">
      <%= @post.body.html_safe %>
      <%= render partial: "blog/comments/comment", post: @post %>
    </div>
  </div>
  <div class="large-4 columns sidebar">
    sidebar
  </div>
</div>

评论部分形式

<%= form_for [:blog,@comment] do |f| %>
  <%= render 'blog/shared/error_messages', object: f.object %>

  <div class="field panel">
    <%= f.label :name %><br>
    <%= f.text_field :name,class: 'form-control' %>
  </div>

  <div class="field panel">
    <%= f.label :email %><br>
    <%= f.text_field :email,class: 'form-control' %>
  </div>

  <div class="field panel">
    <%= f.label :body, "Comment" %><br>
    <%= f.text_area :body,class: 'form-control' %>
  </div>

  <% if logged_in? %>
    <%= f.hidden_field :user_id, value: current_user.id %>
  <% end %>

  <%= f.hidden_field :post_id %>



  <div class="actions">
    <%= f.submit "Create comment", class:"btn btn-danger btn-block" %>
    <a class="btn btn-warning btn-block cancel">Cancel</a>
  </div>
<% end %>

部分错误消息

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

我尝试对评论模型进行验证,但它没有显示它们,即使它正确地重定向回来。

我知道我必须使用渲染而不是重定向,但我不知道要渲染什么。这是我想弄清楚的,因为我没有渲染新动作。

重写您的 Blog::CommentsController 控制器 create 操作,使其重新呈现表单而不是重定向到上一页。这样,错误将呈现在页面上。

class Blog::CommentsController < Blog::BaseController

  def create
    @comment = Comment.new(comment_params)
    if @comment.save
      flash[:success] = "Comment successfully created!"
      redirect_to blog_post_url(@comment.post)
    else
      flash[:warning] = "Something went wrong, try again. If problem persists please let our team know about it!"
      @post = @comment.post
      render 'blog/post/show'
    end
  end

end

通过这种方式,您可以重新呈现 CommentsController 中的 show 操作并明确为其提供正确的 @post 以便它在视图中可用。