2012-03-07 8 views
0

に掲載されたことを通知ユーザーに電子メールを送信します。実際には、Profileモデルは、それ:has_many :superlativesSuperlative:belongs_to :profileように設定されています。私がしたいのは、彼らのプロフィールが新しい最上級を受け取ったときに電子メールを送信することです。私はこの仕事をするためにメーラーとメーラーの行動に何を渡すべきか分かりません。誰かが私を助けることができますか?は3アプリ、人のプロフィールは最上級を持つことができ、新たな何かが私のRailsでは自分のプロフィール

私は他の電子メールを送信するAction MailerにRailsCastを追ってきたので、私のメーラーもののすべてはコントローラです。私は何が起こっているのか分かりません。私は新しく作成された最上級を使ってユーザーの電子メールを見つけるためのユーザープロファイルを見つける必要があることを知っています(これは簡単な方法があると思います)Superlatives#createアクションに@profileを割り当てようとしました。。私のsuperlatives_controllerで言われて、私はかなり試したし、以下の私のコードを掲載している旨のエラー

マイcreateアクション:

def create 
    @superlative = Superlative.new(params[:superlative].merge(:author_id => current_user.id)) 
    if @superlative.save! 
    SuperlativeMailer.new_superlative(@user).deliver 
    respond_to do |format| 
     format.js { } 
    end 
    else 
    respond_to do |format| 
     format.js { render 'fail_superlative_create.js.erb' } 
    end 
    end 
end 

そして、私のSuperlativeMailerのnew_superlativeアクション:

def new_superlative(user) 
    @superlative = superlative 
    @profile = superlative.profile 
    @user = superlative.profile.user 
    mail(:to => user.email, :subject => "#{@superlative.name} just gave you a superlative.") 
end 

は、しかし、このすべてで、私はエラーが表示されます。

NameError (undefined local variable or method `superlative' for #<SuperlativeMailer:0x103c006a8>): 
    app/mailers/superlative_mailer.rb:6:in `new_superlative' 
    app/controllers/superlatives_controller.rb:8:in `create' 

答えて

1

あなたが代わりにUser

def new_superlative(superlative) 
    @superlative = superlative 
    @profile = superlative.profile 
    @user = superlative.profile.user 
    mail(:to => @user.email, :subject => "#{@superlative.name} just gave you a superlative.") 
end 

とコントローラで

のメーラーに Superlativeオブジェクトを渡す必要があります

SuperlativeMailer.new_superlative(@superlative).deliver

+0

パーフェクト、ありがとう! – tvalent2

関連する問題