0

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

我在顶部包含了 url_helpers(不确定它是否在正确的位置)。我得到的错误是:NoMethodError: undefined methodlink_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
4

1 回答 1

1

尝试使用ActionController::Base.helpers.link_to,而不是仅仅link_to因为助手不能用于lib.

于 2016-09-05T11:35:35.020 回答