在rake任务中使用link_to会给出一个no方法 - 包括帮助程序不起作用

时间:2016-09-05 10:33:47

标签: ruby-on-rails intercom

我有一个rake任务,我希望使用对讲机向用户发送电子邮件。我遇到的问题是link_to方法不存在。

我在顶部包含了url_helpers(不确定它是否正确的位置)。我得到的错误是:NoMethodError: undefined method link_to'for main:Object`

如果可能,如果我做错了,请告诉我。

这是rake任务:

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

1 个答案:

答案 0 :(得分:0)

尝试使用ActionController::Base.helpers.link_to代替link_to,因为lib无法使用帮助者。