Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:电子邮件的自定义模板;交付;方法_Ruby On Rails_Email - Fatal编程技术网

Ruby on rails Rails:电子邮件的自定义模板;交付;方法

Ruby on rails Rails:电子邮件的自定义模板;交付;方法,ruby-on-rails,email,Ruby On Rails,Email,我正在构建一个电子邮件系统,将不同的电子邮件存储在数据库中,并通过method\u missing调用相应的“deliver\u”方法(因为这些方法是用户生成的,所以我无法显式声明) 我的问题是,我的rails应用程序仍然试图为生成的电子邮件呈现模板,尽管这些模板不存在。我想强制所有电子邮件使用相同的模板(views/test\u email.html.haml),该模板将被设置为从我的数据库记录中提取格式 我怎样才能做到这一点?我尝试在emailer\u controller中的test\u

我正在构建一个电子邮件系统,将不同的电子邮件存储在数据库中,并通过
method\u missing
调用相应的“deliver\u”方法(因为这些方法是用户生成的,所以我无法显式声明)

我的问题是,我的rails应用程序仍然试图为生成的电子邮件呈现模板,尽管这些模板不存在。我想强制所有电子邮件使用相同的模板(
views/test\u email.html.haml
),该模板将被设置为从我的数据库记录中提取格式

我怎样才能做到这一点?我尝试在
emailer\u controller
中的
test\u email
方法中添加
render:template=>“test\u email”
,但没有成功


型号/emailer.rb

class Emailer < ActionMailer::Base

  def method_missing(method, *args)
    # not been implemented yet
    logger.info "method missing was called!!"
  end

end
class EmailerController < ApplicationController

  def test_email
    @email = Email.find(params[:id])
    Emailer.send("deliver_#{@email.name}")
  end

end
class Emailer < ActionMailer::Base

  def method_missing(method, *args)
    logger.info "method missing was called!!"
    recipients  "Test <test@test.com>" 
    body        "#{ Email.find_by_name(method.to_s).body }"
  end

end
views/emails/index.html.haml

%h1 Listing emails
%table{ :cellspacing => 0 }
  %tr
    %th Name
    %th Subject
  - @emails.each do |email| 
    %tr
      %td=h email.name
      %td=h email.subject
      %td= link_to 'Show', email
      %td= link_to 'Edit', edit_email_path(email)
      %td= link_to 'Send Test Message', :controller => 'emailer', :action => 'test_email', :params => { :id => email.id }
      %td= link_to 'Destroy', email, :confirm => 'Are you sure?', :method => :delete
%p= link_to 'New email', new_email_path
我在上面遇到的错误:

缺少模板

缺少模板 查看中的\u database.erb中的\u email\u的emailer/name\u 路径应用程序/视图


尝试多部分可能会奏效

  def test_email
    @email = Email.find(params[:id])
    Emailer.send("deliver_#{@email.name}")
    part :content_type => 'multipart/alternative' do |copy|
      copy.part :content_type => 'text/plain' do |plain|
        plain.body = render( :file => "conta.text.plain.erb",  :email=>@email )
      end
      copy.part :content_type => 'text/html' do |html|
        html.body = render( :file => "conta.text.html.erb",  :email => @email )
      end
    end
  end

啊,我觉得很傻。我想出来了:

型号/emailer.rb

class Emailer < ActionMailer::Base

  def method_missing(method, *args)
    # not been implemented yet
    logger.info "method missing was called!!"
  end

end
class EmailerController < ApplicationController

  def test_email
    @email = Email.find(params[:id])
    Emailer.send("deliver_#{@email.name}")
  end

end
class Emailer < ActionMailer::Base

  def method_missing(method, *args)
    logger.info "method missing was called!!"
    recipients  "Test <test@test.com>" 
    body        "#{ Email.find_by_name(method.to_s).body }"
  end

end
class-Emailer
由于传入的
方法
基本上是记录的名称,我可以直接提取存储在数据库中的正文内容,并将其作为电子邮件正文传递进来,完全绕过模板。

我会“升级”

换句话说,对每个人的电子邮件使用相同的视图。但在视图中,根据用户id呈现不同的文本

请记住,视图可以调用render方法

在我的例子中,我允许用户使用liquid模板系统上传电子邮件模板。(谷歌搜索rails liquid模板。)使用liquid很好,因为它是安全的——用户不能在模板中包含任意的ruby代码

然后,我的电子邮件视图呈现液体模板,并由此生成自定义电子邮件