使用简单形式创建对象时出错

时间:2016-11-21 14:10:28

标签: ruby-on-rails ruby ruby-on-rails-4 simple-form

我正在创建使用简单表单发布的评论。它看起来像这样:

  

视图/帖/ show.html.slim

# ...

- if user_signed_in?
  .row
    .columns
      h2 Add a comment:

  .row
    .medium-5.columns
      = simple_form_for [post, comment] do |f|
        = f.error_notification

        .form-inputs
          = f.input :body

        .form-actions
          = f.button :submit

此外,我在评论模型中进行了验证:

validates :body, presence: true

当我尝试创建空注释时,我需要在字段下面出错。 这应该是 '在身体评论字段下不能为空白'

这是我的评论控制器(我使用了响应者和decent_exposure)

  

comments_controller.rb

class CommentsController < ApplicationController
  before_action :authenticate_user!, only: :create
  before_action :authorize_user!, only: :destroy

  expose :post
  expose_decorated :comment

  def create
    comment.post = post
    comment.user = current_user
    if comment.save
      respond_with comment, location: post
    else
      # I should to write here something but I don't know what
    end
  end

  def destroy
    comment.destroy
    respond_with comment, location: post
  end

  private

  def authorize_user!
    authorize(comment, :manage?)
  end

  def comment_params
    params.require(:comment).permit(:body, :post_id)
  end
end

我不知道我应该检查一下这样的评论吗?如果是的话,我应该写什么呢?

0 个答案:

没有答案