2016-09-05 24 views
0

我有一個rake任務,我想要使用對講機向用戶發送電子郵件。我遇到的問題是link_to方法不存在。在rake任務中使用link_to會導致無法使用 - 包括幫助程序不起作用

我在頂部包含了url_helpers(不知道它是否合適)。我得到的錯誤是:NoMethodError: undefined method link_to'for main:Object`

請讓我知道這是否可能,如果我做錯了什麼。

這裏是耙任務:

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

您是否嘗試將此代碼移入模板,然後從rake任務中調用模板? – DMH

+0

@DMH我不知道這是怎麼完成的...... –

+0

@dkp當我嘗試在heroku上部署時出現此錯誤「TypeError:錯誤的參數類型ActionView :: Base(預期的模塊」 –

回答

0

嘗試,因爲助手不提供給lib使用ActionController::Base.helpers.link_to而不只是link_to