Ruby on rails rails中的电子邮件撰写视图

Ruby on rails rails中的电子邮件撰写视图,ruby-on-rails,ruby-on-rails-4,model-view-controller,devise,actionmailer,Ruby On Rails,Ruby On Rails 4,Model View Controller,Devise,Actionmailer,我已经创建并希望建立一个邮箱 我不想发送模板,而是想通过电子邮件发送视图中表单textarea的内容 我需要一个视图来编辑消息,它应该被发送到控制器,控制器反过来调用我的邮件程序中的send_mail方法 Class Notifier < ActionMailer::Base default from: "from@example.com" def send_email( email, subject, body ) mail( :to => email,

我已经创建并希望建立一个邮箱

我不想发送模板,而是想通过电子邮件发送视图中表单textarea的内容

我需要一个视图来编辑消息,它应该被发送到控制器,控制器反过来调用我的邮件程序中的send_mail方法

Class Notifier < ActionMailer::Base
  default from: "from@example.com"

  def send_email( email, subject, body )
  mail(
    :to => email,
    :subject => subject
  ) do |format|
    format.text { render :text => body }
  end
end

end
我的看法是:

 <%= form_for(:post, :url => {:action => 'send'}) do |f| %>
    <%= f.text_field(:title, class: 'form-control')%>
    <%= f.text_area(:content, rows: 15)%>
    <%= f.button "Submit", type: 'submit' %>
<% end %>
问题是,当使用rails g mailer Notifier生成邮件程序时

我在mailers中得到一个notifier.rb,在views中得到一个notifier文件夹。但是,我没有用于通知程序视图的视图控制器


问题:如何在我的视图和以电子邮件形式发送输入文本之间建立链接?

您需要创建一个控制器来处理您的视图,并且在该控制器中您需要像这样调用邮件程序:您需要更改表单字段的名称以匹配调用中的参数,反之亦然

  Notifier::send_email( params[:email], params[:subject], params[:body]).deliver
我建议你看看这些铁路乘客:


您需要创建一个处理视图的控制器,在该控制器中,您需要像这样调用邮件程序:您需要更改表单字段的名称以匹配调用中的参数,反之亦然

  Notifier::send_email( params[:email], params[:subject], params[:body]).deliver
我建议你看看这些铁路乘客:


因此,请将notifier.rb视为控制器。您在其中定义了“发送邮件”。这意味着在视图/通知程序中,您应该添加一个send_mail.html.haml erb/slim。。。品味问题将成为邮件的主体

现在,从接收表单的控制器,您只需调用

 Notifier.send_mail(email, subject, body).deliver

因此,请将notifier.rb视为控制器。您在其中定义了“发送邮件”。这意味着在视图/通知程序中,您应该添加一个send_mail.html.haml erb/slim。。。品味问题将成为邮件的主体

现在,从接收表单的控制器,您只需调用

 Notifier.send_mail(email, subject, body).deliver

这可能是制作非ActiveRecord模型的好地方。我知道现在一个问题已经解决了,这有点超出了范围,但它很有用,为什么不呢

我建议您查看并构建一个表单模型通知?它封装了存储表单内容、验证表单内容和发送实际电子邮件的过程。请注意,本文中的实现已经过时,Rails 4引入了ActiveModel::Model,简化了该过程

优点:

另一个类主要以声明式样式定义,易于阅读和查找 可以通过SimpleForm或Rails的表单助手轻松、干净地进行布局 获取传统Rails模型的所有好处,如验证和失败时的错误 在语义上,代码看起来与应用程序中使用DB或其他任何东西的其余部分一致 缺点:

另一类,可以认为是过度工程化 更多的代码,更多的工作,易于维护是有争议的 要在呈现此窗体的控制器中创建的另一个对象 一旦完成,使其工作的过程与使任何其他资源工作的过程几乎相同。我想,这封邮件在它的单独页面上

路线:

resource :notification, only: [:create] do
  get :new, path: "" # A matter of taste, really
  # You may use the default `new` route
  # with a path `notifications/new`
end
控制器:

class NotificationsController
  def new
    @notification = Notification.new
  end

  def create
    @notification = Notification.new(notification_params)
    if @notification.send
      # success! redirect somewhere?
    else
      render :new # render the form again with the errors
    end
  end

  private
  def notification_params
    params.require(:notification).permit(:email, :subject, :body)
  end
end
您还需要一个视图,用于将@notification呈现为表单的新操作。只有新建,创建不需要自己的。现在来看有趣的部分,模特:

class Notification # Yep, it inherits nothing!
  include ActiveModel::Model

  attr_reader :email, :subject, :body

  validates :email,
    presence: true # You might want to validate its format?
  validates :subject,
    presence: true, length: {in: 0..100} # Too long subjects are annoying
  validates :body,
    presence: true

  def persisted?
    false # I have no idea why, but it's defined in the article, no harm done
    # I'd love to hear the explaination about this though
  end

  def send
    if valid? # no objections from validations?
      # Alright, send it already!
      Notifier.send_mail(email, subject, body).deliver
      # thanks for this line go to @Daniel and his answer
      true
    else
      false
    end
  end
end
最后,一个专业提示:Rails 4.2现在正处于最前沿!引入了ActiveJob,它与邮件程序集成。通过将call to deliver方法替换为call to deliver_,您将使电子邮件排队,以便由后台任务处理器发送,如下所述。我真的不认为是时候把它用在新的地方了,但是考虑一下这一点对于未来的项目。


我真的认为它好吗?是的,我真的这么做了,我重构了一个用户密码更改器,使代码变得更易于导航和查看。

这可能是制作非ActiveRecord模型的好地方。我知道现在一个问题已经解决了,这有点超出了范围,但它很有用,为什么不呢

我建议您查看并构建一个表单模型通知?它封装了存储表单内容、验证表单内容和发送实际电子邮件的过程。请注意,本文中的实现已经过时,Rails 4引入了ActiveModel::Model,简化了该过程

优点:

另一个类主要以声明式样式定义,易于阅读和查找 可以通过SimpleForm或Rails的表单助手轻松、干净地进行布局 获取传统Rails模型的所有好处,如验证和失败时的错误 在语义上,代码看起来与应用程序中使用DB或其他任何东西的其余部分一致 缺点:

另一类,可以认为是过度工程化 更多的代码,更多的工作,易于维护是有争议的 要在呈现此窗体的控制器中创建的另一个对象 一旦完成,使其工作的过程与使任何其他资源工作的过程几乎相同。我想,这封邮件在它的单独页面上

路线:

resource :notification, only: [:create] do
  get :new, path: "" # A matter of taste, really
  # You may use the default `new` route
  # with a path `notifications/new`
end
控制器:

class NotificationsController
  def new
    @notification = Notification.new
  end

  def create
    @notification = Notification.new(notification_params)
    if @notification.send
      # success! redirect somewhere?
    else
      render :new # render the form again with the errors
    end
  end

  private
  def notification_params
    params.require(:notification).permit(:email, :subject, :body)
  end
end
您还需要一个 将@notification呈现为表单的新操作。只有新建,创建不需要自己的。现在来看有趣的部分,模特:

class Notification # Yep, it inherits nothing!
  include ActiveModel::Model

  attr_reader :email, :subject, :body

  validates :email,
    presence: true # You might want to validate its format?
  validates :subject,
    presence: true, length: {in: 0..100} # Too long subjects are annoying
  validates :body,
    presence: true

  def persisted?
    false # I have no idea why, but it's defined in the article, no harm done
    # I'd love to hear the explaination about this though
  end

  def send
    if valid? # no objections from validations?
      # Alright, send it already!
      Notifier.send_mail(email, subject, body).deliver
      # thanks for this line go to @Daniel and his answer
      true
    else
      false
    end
  end
end
最后,一个专业提示:Rails 4.2现在正处于最前沿!引入了ActiveJob,它与邮件程序集成。通过将call to deliver方法替换为call to deliver_,您将使电子邮件排队,以便由后台任务处理器发送,如下所述。我真的不认为是时候把它用在新的地方了,但是考虑一下这一点对于未来的项目。

我真的认为它好吗?是的,我真的这么做了,我重构了一个用户密码更改器,使代码变得更易于导航和查看