如何渲染形式partial - Rails的验证错误

时间:2015-10-27 14:41:17

标签: ruby-on-rails ruby ruby-on-rails-4 parameters partials

我正在构建自定义博客。我有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 %>

我尝试在评论模型上放置验证,但即使它正确地重定向也不会显示它们。

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

1 个答案:

答案 0 :(得分:2)

重写您的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

这样您就可以从show重新呈现CommentsController操作,并明确地为其提供正确的@post,以便在视图中显示。