user1924165
user1924165

Reputation: 109

Send Email Notice in Rails App Upon Comment Creation

I am using the gem 'foreigner' and setup comment for my app and everything works. However, I would also like to notify my users whenever a comment is created. I've got two users, customers and developers. Customers can post comments and Developers can post comments.

How would I setup my comments_controller.rb file to figure out if its a customer or developer posting the comment and then send email with the right template.

So far I've tried the following with no work;

  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" and "customer_notify" being the class defined in my Notifier mailer.

My "Notifier" mailer looks like this so far;

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

@Joblisting is the job that is referenced to as each Joblisting has its own comments from customers and developers.

Doing the above, gives me an error - undefined method 'current_user' for nil:NilClass

So I'm guessing it's not finding the Joblisting ID nor is it finding the customers email address, then if the customer posted a comment for that same job, it would send email to the developer with notification of a new comment posted.

Any suggestions?

Upvotes: 0

Views: 385

Answers (1)

hyperrjas
hyperrjas

Reputation: 10744

you have passing joblisting from your controller:

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

on notifier mailer

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

Regards!

Upvotes: 1

Related Questions