2016-09-05 7 views
0

私は、インターコムを使用しているユーザに電子メールを送信したいレーキタスクを持っています。私が持っている問題は、link_toメソッドが存在しないということです。rakeタスクでlink_toを使用すると、noメソッドが発生します。ヘルパーは機能しません。

url_helpersを先頭に含めました(正しい場所がわからない場合)。私が得ているエラーは:NoMethodError: undefined method link_to for main:Object`

これが可能かどうか、私が間違ったことをしているかどうか教えてください。ヘルパーはlibに利用できないので、

include Rails.application.routes.url_helpers 

namespace :example do 
desc "TODO" 
task affiliate_bonus: :environment do 

User.all.each do |user| 
    if user.invited && user.invited.size > 0 
    user_affiliate_bonus = AffiliateBonu.where(inviter_id: user.id) 

    @the_user_af_bonus = 0 
    @total_affiliate_bonus = 0 

    user_affiliate_bonus.each do |ab| 
     @total_affiliate_bonus = @total_affiliate_bonus.to_f + ab.btc_bonus_amount 

     if ab.fulfilled == false 
     @the_user_af_bonus = @the_user_af_bonus.to_f + ab.btc_bonus_amount 

     if user.btc_wallet_address 
      ab.fulfilled = true 
      ab.save! 
     end 
     end 
    end 

    if @the_user_af_bonus >= 0 && user.affiliate_subscription 

     @intercom = Intercom::Client.new(app_id: ENV["INTERCOM_APP_ID"], api_key: ENV["INTERCOM_APP_KEY"]) 
     user_receive = @intercom.users.create(email: user.email, user_id: user.id, :signed_up_at => Time.now.to_i) 

     referral_link_text = link_to("https://www.example.com/ref/#{user.referral_link}", "https://www.example.com/ref/#{user.referral_link}") 
     unsubscribe_text = link_to("click here", {:controller => "users", :action => "affiliate_unsubscribe", :user_id => user.id }) 
     balance_text = link_to('Balance', "https://example.com/users/balance") 

     text = 
     " 
     <% if user.first_name %> 
     <p>Dear <%= user.first_name %>,</p> 
     <% else %> 
     <p>Dear Customer,</p> 
     <% end %> 

     <p> 
      Your referral link: #{referral_link_text} has generated $ <%= @bonus %> for you this week. 
      <% if @bonus > 0 %> 
      This amount was sent to your #{balance_text}. 
      <% end %> 
     </p> 

     <p>Regards,</p> 

     <p>George @<strong>Kaboom</strong></p> 

     <p>--<br> 
     PS. You are receiving this email update because your link has been used to sign up.</p> 

     <p>To unsubscribe from your profit reports, #{unsubscribe_text}.</p> 
     " 

     @intercom.messages.create({ :message_type => 'email', subject: "Your Weekly Referral Earnings Report", :body => text, :template => "plain", :from => { :type => 'admin', :id => "55070" }, :to => { type: "user", :id => user_receive.id } }) 
    end 
    end 
end 
end 
end 
+0

このコードをテンプレートに移動し、レーキタスク内からテンプレートを呼び出そうとしましたか? – DMH

+0

@DMHどうすればいいのか分かりません... –

+0

@dkp herokuに展開しようとするとこのエラーが発生します。 "TypeError:間違った引数タイプActionView :: Base(期待されるモジュール" –

答えて

0

ActionController::Base.helpers.link_toだけではなくlink_toを使用してみてください:ここ

はrakeタスクです。

関連する問題