2013-01-22 28 views
0

我使用gem'foreigner'和設置評論爲我的應用程序和一切正常。不過,我還想在創建評論時通知我的用戶。我有兩個用戶,客戶和開發人員。客戶可以發表評論,開發者可以發表評論。在Rails應用程序中發送電子郵件通知創建

我將如何設置我的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是參考的工作,因爲每個Joblist都有自己的評論,來自客戶和開發人員。

做好以上,給我一個錯誤 - undefined method 'current_user' for nil:NilClass

所以我猜它沒有找到Joblisting ID也不是尋找客戶的電子郵件地址,然後如果客戶張貼同一作業的評論,它會發送電子郵件給開發人員並通知發佈新評論。

有什麼建議嗎?

回答

1

你傳遞joblisting從你的控制器:

的通知郵件

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

問候

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 

+0

嗨,謝謝!我仍然必須做錯事。我承認,電子郵件的事情,我仍然沒有頭腦纏繞...我現在正式獲得正式的參數不能成爲實例變量 def developer_notify(@developer,joblisting)遵循上面的示例。 – user1924165

+0

我不知道你是如何有模型之間的關係,但如果錯誤'未定義的方法'current_user'爲零:NilClass'已經修復,你可以接受這個迴應,並且,你必須對這個最後的錯誤提出其他問題。您必須添加更多信息,例如模型關係。 – hyperrjas

+0

我已經想通了。謝謝! – user1924165

相關問題