ruby-on-rails – 将params传递给rails action mailer

我正在使用动作邮件程序向电子邮件获胜者发送电子邮件,@result数组包含已赢得包括电子邮件在内的参与者记录.

@result=>[#<Participant id: 47, admin_id: nil, sweepstake_id: 8, participant_name: "gomez", participant_email: "rakesh.k@birdvision.in", participant_number: "12131245421", ip_addr: "127.0.0.1", answer: "nice", reg_time: nil, entry_opt_id: nil, created_at: "2015-09-03 12:15:48", updated_at: "2015-09-07 12:12:53">, #<Participant id: 47, admin_id: nil, sweepstake_id: 8, participant_name: "gomez", participant_email: "rakesh.k@birdvision.in", participant_number: "12131245421", ip_addr: "127.0.0.1", answer: "nice", reg_time: nil, entry_opt_id: nil, created_at: "2015-09-03 12:15:48", updated_at: "2015-09-07 12:12:53">]

但是当我将结果从我的控制器传递给邮件程序时,结果现在只有id作为字符串.
参数:{“result”=> [“47”,“47”]}.
如何传递包含所有参与者记录的数组结果?

视图页面中的代码:

<%= link_to "Email Winners",email_winner_sweepstakes_path(:result=>@result) %>

控制器中的代码:

def email_winner
      Rails.logger.info("***********************#{params.inspect}")
      ParticipantMailer.winner_confirmation(params[:result]).deliver_now!

  end

邮件中的代码:

def winner_confirmation(result)
   @result = result
   @url  = 'http://example.com/login'
   Rails.logger.info("=================mailer=================")
   Rails.logger.info(@result.inspect)
   @result.each do |i|
   mail(to: i.participant_email, subject: 'Congratulation')
end
    end

最佳答案 你必须按ID找到获胜者,在这一行:

<%= link_to "Email Winners", email_winner_sweepstakes_path(result: @result) %>

你所做的是制定URL,你不能将对象存储为唯一的字符串,它所做的就是在@result上调用.id

解决这个问题:

在你的控制器中:

def email_winner
  @results = Participant.where("id in (?)", params[:result])
  ParticipantMailer.winner_confirmation(@results).deliver_now!
end
点赞