简体   繁体   English

将params传递给rails动作邮件程序

[英]Passing params to rails action mailer

I am using action mailer to email winners, @result array has records of participants who has won including their email. 我正在使用动作邮件程序向电子邮件获胜者发送电子邮件,@ @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">]

But when i pass the result from my controller to the mailer, the result now only has id as string. 但是当我将结果从我的控制器传递给邮件程序时,结果现在只有id作为字符串。 Parameters: {"result"=>["47", "47"] }. Parameters: {"result"=>["47", "47"] }。 How can i pass the array result which has all the participant records? 如何传递包含所有参与者记录的数组结果?

Code in view page: 视图页面中的代码:

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

Code in controller: 控制器中的代码:

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

  end

Code in mailer: 邮件中的代码:

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

You will have to find winners by ID, in this line: 你必须按ID找到获胜者,在这一行:

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

what you do is formulate the URL and you can't store objects its only string and what it did is it will call .id on @result 你所做的是制定URL,你不能将对象存储为唯一的字符串,它所做的就是在@result上调用.id

to fix that: 解决这个问题:

in your controller: 在你的控制器中:

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

You cannot pass ruby objects in links, because that triggers a new page load on a different URI, and the ids are provided in that address. 您无法在链接中传递ruby对象,因为这会在不同的URI上触发新的页面加载,并且在该地址中提供了ID。 You need to only pass ids. 你只需要传递id。

Therefore your ids being passed here, you can fetch the objects with the following: 因此,您的ID在此处传递,您可以使用以下内容获取对象:

@result.each do |id|
  participant = Participant.find(id)
  mail(to: participant.participant_email, subject: 'Congratulation')
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM