D
D
dexdev2014-02-04 17:39:37
Ruby on Rails
dexdev, 2014-02-04 17:39:37

How to properly send an email with dynamic data in rails?

There is a Post sign

Post.rb
has_many :responces
has_many :users, through: :responces
end

There is a sign Responce
class Responce < ActiveRecord::Base
  belongs_to :user
  belongs_to :post
end

It is necessary that when adding a record to the Responce, a letter with data from the Responce should be sent to the Post user
in the Responce controller
def create
    @post = Post.find(params[:post_id])
    @responce = @post.responces.build(responce_params)
    @responce.user = current_user
    if @responce.save
      NotificationMailer.new_responce(@post.user).deliver
      		flash[:success] = "Привет"
      		redirect_to post_path @post
  		else
    		render 'new'
  		end
  end

in the body of the letter
<%= @post.responce.price %> @post.responce.user.name

That is, I want the user who created the Post to send the name of the user and the price of the user who created the Responce, but I’m not good at it, and I just can’t make friends with OR connections!
Help please, I use rails 4 ruby ​​2
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tonis Simo, 2014-02-04
@AdilA

Responce is in what language? :)
In NotificationMailer :

def new_responce(responce)
  @responce = responce
  @responce_user = @responce.user
  mail to: @responce.post.user.email #, subject: "Subject",  ...
end

In the body of the letter:
In the controller sending the letter:
This is all without optimizing queries to the database.
In general, these are the basics, smoke more mana, especially about the logic of models.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question