如何传递数组表单邮件来查看?

时间:2015-10-07 07:51:56

标签: ruby-on-rails ruby-on-rails-4

请帮助传递数组表格邮件进行查看。

应用程序/邮寄者/ send_mailer.rb:

class SendMailer < ApplicationMailer
  default from: 'no-reply@kalinin.ru'

  def sends_send(sends, emails, current_user_email)
    @log = []

    @log.push('--------------------------------------------------------------------')
    @log.push('send log for ' + current_user_email + ' :: started at ' + Time.now.to_s)

    sends.each do |send|
      emails.each do |email|
        @send = send
        @email = email

        if mail(to: email.email, subject: send.subject)
          @log.push('OK:: email: ' + email.email + ' :: send: ' + send.id.to_s + ' :: send_at: ' + Time.now.to_s)
        else
          @log.push('FAIL:: email: ' + email.email + ' :: send: ' + send.id.to_s + ' :: send_at: ' + Time.now.to_s)
        end
      end  
    end  
    puts @log
  end  
end

应用程序/控制器/ sends_controller.rb:

def send_up
  @sends = Send.where(user_id: current_user.id) 
  @emails = Email.where(user_id: current_user.id) 

  SendMailer.sends_send(@sends, @emails, current_user.email).deliver_now

  redirect_to log_index_path
end

在主页上按下按钮后,执行动作&#39; send_up&#39;并执行send_mailer.rb。数组@log被填充并输出到控制台。但我需要在视图中显示@log。但现在看来是空的

应用程序/视图/数/ index.rb:

<h1>Log#index</h1>
<%= @log %>

路线:

Rails.application.routes.draw do
  get 'log/index'
  ........
  ........
  ..........
end

rake routes:

log_index GET    /log/index(.:format)                      log#index

请帮助将@log传递给视图。

1 个答案:

答案 0 :(得分:3)

模型方法中定义的实例变量在控制器或调用该模型方法的任何其他类中都不可用。但是,如果需要,可以在方法结束时返回变量的值,然后确保将其他变量设置为等于该值。例如,在sends_sends(这是一个非常容易混淆的方法btw)方法中,将最后一行从puts @log更改为@log

然后在控制器中,更改

SendMailer.sends_send(@sends, @emails, current_user.email).deliver_now

@log = SendMailer.sends_send(@sends, @emails, current_user.email).deliver_now