0

我正在为我的应用程序使用 gem 'foreigner' 和设置评论,一切正常。但是,我还想在创建评论时通知我的用户。我有两个用户,客户和开发人员。客户可以发表评论,开发者可以发表评论。

我将如何设置我的 comments_controller.rb 文件来确定是客户还是开发人员发布了评论,然后使用正确的模板发送电子邮件。

到目前为止,我已经尝试了以下没有工作;

  def create
    @comment = Comment.new(params[:comment])

    respond_to do |format|
      if @comment.save
        if current_user.is_developer?
           Notifier.developer_notify(@developer).deliver
        elsif current_user.is_customer?
           Notifier.customer_notify(@customer).deliver
        end
        format.html { redirect_to :back, notice: 'Comment was successfully created.' }
        # format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        # format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

“developer_notify”和“customer_notify”是我的通知程序邮件中定义的类。

到目前为止,我的“通知程序”邮件看起来像这样;

  def developer_notify(joblisting)
    @joblisting = joblisting
    mail(:to => @joblisting.current_user.email, :subject => "There's a new comment.")
  end

@Joblisting 是被引用的工作,因为每个 Joblisting 都有来自客户和开发人员的评论。

执行上述操作,给我一个错误 -undefined method 'current_user' for nil:NilClass

所以我猜它没有找到 Joblisting ID,也没有找到客户的电子邮件地址,那么如果客户对同一个工作发表了评论,它会向开发人员发送电子邮件,通知发布了新的评论。

有什么建议么?

4

1 回答 1

1

你已经joblisting从你的控制器传递过来:

def create
 @comment = Comment.new(params[:comment])
 #you have define here joblisting for example:
  joblisting = JobListing.first #adapt the query to your needs
 respond_to do |format|
  if @comment.save
    if current_user.is_developer?
       #here add joblisting as argument after @developer
       Notifier.developer_notify(@developer, joblisting).deliver
    elsif current_user.is_customer?
       #here add joblisting as argument after @developer
       Notifier.customer_notify(@customerm, joblisting).deliver
    end
    format.html { redirect_to :back, notice: 'Comment was successfully created.' }
    # format.json { render json: @comment, status: :created, location: @comment }
  else
    format.html { render action: "new" }
    # format.json { render json: @comment.errors, status: :unprocessable_entity }
  end
 end
end

在通知邮件程序上

def developer_notify(@developer, joblisting)
    @joblisting = joblisting
    mail(:to => @joblisting.current_user.email, :subject => "There's a new comment.")
  end

问候!

于 2013-01-22T20:56:26.423 回答