嵌套if语句中的意外输入结束

时间:2015-04-12 03:57:57

标签: ruby-on-rails ruby if-statement

我正在使用act-as-commentable gem,我想在留下评论时发送通知电子邮件 - 具体取决于记录的类型。

def send_comment_notification_email
    klass = Object.const_get commentable_type
    resource = klass.find_by(id: commentable_id)

    if parent_id.nil?

      if commentable_type == "Review" 
        # CommentMailer.new_review_comment(resource, self).deliver
      elsif commentable_type == "Movie"
        if !resource.company_id.nil?
          CommentMailer.new_company_stack_comment(resource, self).deliver
        end
        if resource.owner_type == 'User'
          CommentMailer.new_personal_stack_comment(resource, self).deliver
        end
      elsif commentable_type == "MovieItem"
        unless resource.stack.owner.nil?    
          # CommentMailer.new_stack_item_comment(resource, self).deliver
        end
      end

    end   

  end

为什么我收到错误:

  

SyntaxError - 语法错误,意外的输入结束,期待keyword_end:

我可以不嵌套if这样的语句吗?

1 个答案:

答案 0 :(得分:0)

错误是由于在第10行的嵌套if中缺少end。我建议你在那里使用内联条件。您的代码将是这样的:

def send_comment_notification_email
    klass = Object.const_get commentable_type
    resource = klass.find_by(id: commentable_id)

    if parent_id.nil?

      if commentable_type == "Review" 
        # CommentMailer.new_review_comment(resource, self).deliver
      elsif commentable_type == "Movie"
          CommentMailer.new_company_stack_comment(resource, self).deliver unless resource.company_id.nil?
        end
        if resource.owner_type == 'User'
          CommentMailer.new_personal_stack_comment(resource, self).deliver
        end
      elsif commentable_type == "MovieItem"
        unless resource.stack.owner.nil?    
          # CommentMailer.new_stack_item_comment(resource, self).deliver
        end
      end

    end   

  end

您可以阅读内联条件here