【问题标题】:rails call method that should send email应该发送电子邮件的rails调用方法
【发布时间】:2013-11-30 03:04:12
【问题描述】:

所以,我有一个带有一些学生网格的索引页面。通过一个按钮,我会调用一个电子邮件方法来向@students = Student.all的所有学生发送电子邮件

如何调用方法?

方法的参数是@students,对吗?

<%= link_to 'Email', send_to_student_path(@students) %>

正如我所见here

StudentController.rb

  def send_to_student()
   #binding.pry
   @students.each do |student|
     StudentMailer.email_recall(student).deliver
    end
  end

还有邮递员:

class StudentMailer < ActionMailer::Base

    def email_recall(student)
        @url  = 'http://example.com/login'
        mail(to: @student.email, 
            from: current_user.email,
            subject: 'Valid your datas') 

    end
end

在 routes.rb 我有:

resources :students do 

    member do
        post :send_to_student
    end
end

第一个问题: 如何在方法参数中从 @students = Student.all 传递每个学生的 id ?

第二个问题: 如何正确调用send_to_student方法?

在此先感谢

尼古拉斯

【问题讨论】:

  • 对不起,我是@students = Student.all

标签: ruby-on-rails email methods parameter-passing params


【解决方案1】:

1) 你使用它的方式是通过路由传递 ID,例如:students/:id/send - 你可以阅读更多关于 member routing on the Rails documentation 的信息。这将设置params[:id] 的参数,您可以在操作中使用该参数

或者,如果您希望向所有学生发送消息,您可以使用我在下面创建的控制器功能:


2) 我认为您通常可以为此使用Model Method。这是你在模型中放置一个函数的地方,清理你的控制器

我不知道它是否适用于邮件程序,但我会试试这个:

  #app/controllers/students_controller.rb
  def index
      @students = Student.all
      Student.send_to_students(@students)
  end

  #app/models/student.rb
  def self.send_to_student(students)
      students ||= self.all # -> not sure about the self.all call
      students.each do |student|
          StudentMailer.email_recall(student).deliver
      end
  end

【讨论】:

    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    相关资源
    最近更新 更多