简体   繁体   English

Rails 4-Mailer-如何将参数传递给Mailer方法

[英]Rails 4 - mailer - how to pass arguments to a mailer method

I'm trying to figure out how to setup a mailer class in my Rails 4 app. 我试图弄清楚如何在Rails 4应用程序中设置邮件程序类。

I have made a mailer called admin_notes. 我发了一个叫做admin_notes的邮件。 I want to use it to send emails to the internal team when certain actions are taken across the site. 我希望使用它在整个站点上执行某些操作时将电子邮件发送给内部团队。

In my mailer/admin_note.rb, I have: 在我的mailer / admin_note.rb中,我有:

class AdminNote < ApplicationMailer

    def unknown_organisation(organisation_request, user_full_name, name)

     @organisation_request = 
     @user_full_name = 
     @organisation_request.name = 

        # @greeting = "Hi"
    mail( to: "test@testerongmail.com",from: "test@testerongmail.com", subject: "A new organisation")

  end
end

I have an organisation_requests model. 我有一个Organisation_requests模型。 It has: 它具有:

class OrganisationRequest < ActiveRecord::Base
  belongs_to :profile
  delegate :user_full_name, to: :profile, prefix: false, allow_nil: true

The organisation request table has an attribute called :name in it. 组织请求表中有一个名为:name的属性。

When a new organisation request is created, I want to send an admin note to the internal team, alerting someone to start a process. 创建新的组织请求后,我想向内部团队发送管理员说明,提醒某人开始流程。

I'm struggling to figure out how I define the three variables in the mailer method. 我正在努力弄清楚如何在mailer方法中定义三个变量。

I plan to add the send email call to the create action in the organisation requests controller. 我计划将发送电子邮件呼叫添加到组织请求控制器中的create动作。

How can I set these variables? 如何设置这些变量?

Form to create an organisation request is: 创建组织请求的表格为:

<%= simple_form_for(@organisation_request) do |f| %>
            <%= f.error_notification %>
            <div class="form-inputs">
          <%= f.input :organisation_id, collection: @all_organisations << ['other', nil] %>
                </div>

        <div class="form-inputs">
          <%= f.input :name %>
        </div>

                <div class="form-actions">
                    <%= f.button :submit, "Create", :class => 'formsubmit' %>
                </div>
          <% end %>

NEW ATTEMPT: 新尝试:

I have a create action in my organisation controller, I added this service class request for an email: 我的组织控制器中有一个create操作,我为电子邮件添加了此服务类请求:

def create
    @organisation_request = OrganisationRequest.new(organisation_request_params)
    @organisation_request.profile_id = current_user.profile.id

    if @organisation_request.save
      NewOrgRequestService.send_unknown_organisation_requested_flag(organisation_request)
      return redirect_to(profile_path(current_user.profile),
        flash[:alert] => 'Your request is being processed.')
    else
      # Failure scenario below
      @all_organisations    = Organisation.select(:title, :id).map { |org| [org.title, org.id] }

      render :new
    end
  end

I then have a services/organisations requests/NewOrgRequestService.rb 然后,我有一个服务/组织请求/NewOrgRequestService.rb

class OrganisationRequest < ActiveRecord::Base
  class NewOrgRequestService
    attr_accessor :organisation_request

    def self.send_unknown_organisation_requested_flag(organisation_request)
        if @organisation_request.name.present?
            AdminNote.unknown_organisation_requested(organisation_request, user_full_name, name).deliver_later
        end 
      end
  end
end

The AdminNote mailer has: AdminNote邮件程序具有:

class AdminNote < ApplicationMailer
    layout 'transactional_mailer'

    def unknown_organisation_requested(organisation_request, user_full_name, name)

        @organisation_request = @organisation_request
        @user_full_name = @organisation_request.user_full_name
        @name = organisation_request.name

    # @greeting = "Hi"
    mail 
    to: "test@testerongmail.com",from: "test@testerongmail.com", subject: "A new organisation"

  end
end

This doesnt work, but I'm wondering if Im on the right track? 这不起作用,但是我想知道我是否在正确的轨道上? Im not sure if the create action in the controller needs to have some kind of reference to the services/organisation_requests/ path that gets to the file?? 我不确定控制器中的create动作是否需要某种对文件的services / organisation_requests /路径的引用?

I think I may have made a bigger mess than I started with - but I'm out of ideas for things to try next. 我想我可能比开始时陷入了更大的混乱-但是我没有下一步尝试的想法。

This may help you . 这可能对您有帮助

In your mailer method 用您的邮寄方式

def unknown_organisation(org,user)
   @org = org
   @user = user

   mail(to: "test@testerongmail.com",from: "test@testerongmail.com", subject: "A new organisation")
end

In your controller method after saving organization_request and this is how you set your variable. 保存organization_request之后,在您的控制器方法中,这就是设置变量的方式。 You can pass variable you want. 您可以传递所需的变量。

AdminNote.unknown_organization(@organization_request, current_user).deliver_now

In your mailer template access passed value as you do in action view. 像在操作视图中一样,在邮件模板中访问传递的值。 And this is how you use your variable. 这就是您使用变量的方式。

<%= @org.name %>
<%= @org.full_name %>

Hope this helps 希望这可以帮助

If you want to queue message or send later you can use ActiveJob to send mails in the background. 如果要排队消息或以后发送消息,可以使用ActiveJob在后台发送邮件。

For more, see http://guides.rubyonrails.org/active_job_basics.html 有关更多信息,请参见http://guides.rubyonrails.org/active_job_basics.html

I know I am super late but here I go. 我知道我来晚了,但是我走了。

I understand that you are trying to send in some parameters (values) to mailer so that you can use it while sending an email. 我了解您正在尝试向邮件程序发送一些参数(值),以便您可以在发送电子邮件时使用它。

To do so you just need to define a mailer method that accepts some parameters. 为此,您只需要定义一个接受某些参数的mailer方法。 What you have done is right in your AdminNote Mailer unknown_organization method. 您所做的就是正确的AdminNote Mailer unknown_organization方法。

Let's get to your NEW ATTEMPT . 让我们开始您的新尝试 Everything you have done there seems about right except you are passing an undefined variable organization_request . 除了传递未定义的变量organization_request外,您在此处所做的所有事情似乎都是正确的。 You have created an instance variable @organization_request but you are passing something that is not defined. 您已经创建了一个实例变量@organization_request但您传递的是未定义的内容。 Here 这里

NewOrgRequestService.send_unknown_organisation_requested_flag(organisation_request)

That is your first problem. 那是你的第一个问题。 This can be improved as: Your Organizations#create 可以将其改进为:您的组织#create

def create
    @organisation_request = OrganisationRequest.new(organisation_request_params)
    @organisation_request.profile_id = current_user.profile.id

    if @organisation_request.save
      @organisation_request.send_unknown_organisation_requested_flag
      redirect_to(profile_path(current_user.profile),
        flash[:alert] => 'Your request is being processed.')
    else
      # Failure scenario below
      @all_organisations = Organisation.select(:title, :id).map { |org| [org.title, org.id] }

      render :new
    end
  end

And your model can be as follows: 您的模型可以如下所示:

class OrganisationRequest < ActiveRecord::Base
    def send_unknown_organisation_requested_flag
        if self.name.present?
            AdminNote.unknown_organisation_requested(self).deliver_later
        end 
      end
end

I don't know why you are defining a class inside your model class. 我不知道为什么要在模型类中定义一个类。

Your Mailer should look like below: 您的邮件程序应如下所示:

class AdminNote < ApplicationMailer
    layout 'transactional_mailer'

    def unknown_organisation_requested(organisation_request)

        @organisation_request = organisation_request
        @user_full_name = @organisation_request.user_full_name
        @name = organisation_request.name

    # @greeting = "Hi"
    mail 
    to: "test@testerongmail.com",from: "test@testerongmail.com", subject: "A new organisation"

  end
end

There are a lot of typos and method implementation errors here. 这里有很多错别字和方法实现错误。

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

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