actionmailer - where do I define this rails method for an associated model in a mailer? -


how define user method in mailer?

i error:

nomethoderror in appointmentscontroller#create undefined method `user' nil:nilclass 

from line:

mail to: service.user.email, subject: "appointment confirmation" 

i have users many services , each service can have appointment.

when makes appointment, want user owns service appoint made email.

my appointments controller looks this:

before_action :load_services, only: [:new, :edit, :create, :update]  def create   @appointment = appointment.new(appointment_params)   respond_to |format|     if @appointment.save       clientmailer.client_confirmation(@appointment, @service, @user).deliver       format.html { redirect_to @appointment, notice: 'appointment created.' }       format.json { render :show, status: :created, location: @appointment }     else       format.html { render :new }       format.json { render json: @appointment.errors, status: :unprocessable_entity }     end   end end  def load_services   @services = service.all.collect {|service| [ service.title, service.id] } end 

client_mailer.rb looks this:

class clientmailer < actionmailer::base   default from: "bookme@example.com"    def client_confirmation(appointment, service, user)     @appointment = appointment     @service = service     @user = user     @greeting = "hi"      mail to: service.user.email, subject: "appointment confirmation"   end end 

thanks!

the reason don't pass service def client_confirmation.

you're trying pass @service it's not defined anywhere. have @services defined in before action. need set @service filtering @services conditions know about.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -