ruby-on-rails - 如何渲染部分表单的验证错误 - Rails

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

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

我通过对单个帖子的显示操作的部分呈现评论。

帖子 Controller

 class Blog::PostsController < Blog::BaseController

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

end

评论 Controller

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 %>

我尝试对评论模型进行验证,但它不会显示它们,即使它正确地重定向回来也是如此。

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

最佳答案

重写您的 Blog::CommentsController Controller 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 以便它可用在 View 中。

关于ruby-on-rails - 如何渲染部分表单的验证错误 - Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33370921/

相关文章:

javascript - 如何调用js获取合适的元素

ruby-on-rails - 我可以在使用 Active Record 时将 ID 更改为字符串吗?

MySQL 在查询中使用数组

javascript - 如何从 View 中删除逻辑

ruby-on-rails - Heroku:显示隐私错误 "Your connection is not private"

ruby-on-rails - Bundle Install on dev 给出了缺少生产 gem 的错误

sql - Rails - 如何按 ID 范围选择所有记录

ruby-on-rails - Rails - SSL 不适用于裸域

javascript - 单击时将 2 个图像更改为一个文本

ruby-on-rails - 有没有办法解决 Rails 删除的迁移?