【问题标题】:Rails mailer + sendmail from a models来自模型的 Rails 邮件程序 + 发送邮件
【发布时间】:2011-09-02 16:39:58
【问题描述】:

我有一个带有一些附加文件的模型。 在显示操作中,我有一个链接可以打开显示表单的对话框。

此表单用于发送包含父模型的附件以及有关模型的一些信息的电子邮件。

我创建了一个生成器:

rails g mailer notifier
rails g resource emailsystem

现在,我在节目中有一个 div

<div id="dialog" style="display:none">
  <%= form_for ???? do |f| %>
      <%= f.text_field :fromemail %>
  <% end %>
</div>

但是我不知道我需要用什么模型附加这个表格,因为这个表格不需要数据库。 我创建了一个新资源,但是当我使用它时,由于资源表不存在而出现错误。

我该怎么做?我想我需要用当前模型附加对话框表单。

编辑------------

现在我已经创建了我的表单和控制器以及邮件程序 + 模型

mailer + models 

mailer
def plan_notification(resource)
   @plans = resource
   mail(:to => "maskedemail",
      :from => 'maskedemail',
      :subject => 'test')
end

model
def save
    Emailplan.plan_notification(self).deliver!
end

and my controller

def send

  puts 'test'
  @emailplan = Emailplan.save(params[:emailplan])
end


my routes.rb

match 'emailplans/send' => 'emailplans#send', :as => :send_emailplan

my form

<%= form_for @emailplan, :as => :emailplans, :url => send_emailplan_path do |e| %>
    <div>To : <%= e.text_field 'toemail' %></div>
    <div>From : <%= e.text_field 'fromemail' %></div>
    <div>Note : </div>
    <div><%= e.text_area 'note', :rows => 5 %></div>
    <div><%= e.submit 'Send plans' %></div>
<% end %>

但是,我得到了这个错误:

Parameters: {"commit"=>"Send plans", "authenticity_token"=>"12dR2T8IOSoKktQEHxthP8v5bxTuPBzwnoWz9lTgim0=", "utf8"=>"✓", "emailplans"=>{"fromemail"=>"maskedemail", "toemail"=>"maskedemail", "note"=>"23423423"}}
Completed 500 Internal Server Error in 0ms

ArgumentError (wrong number of arguments (2 for 0)):

为什么我的参数数量错误?我可以在哪里设置模型/控制器的参数数量?

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 actionmailer


    【解决方案1】:

    简短的回答是使用 form_tag 而不是 form_for。所以:

    <%= form_tag '/url/to/controller/that/sends/mail/goes/here' do |f| %>
      <%= f.text_field 'fromemail' %>
      <%= f.submit 'Send email' %>
    <%= end %>
    

    更长的答案是您可能想阅读关于 actionmailer 的一两个教程 - 我怀疑您可能对邮件在 rails 中的工作方式有些困惑(?);-)

    此处的文档:http://api.rubyonrails.org/classes/ActionMailer/Base.html

    或者,如果您愿意,RailsCast:http://railscasts.com/episodes/206-action-mailer-in-rails-3

    【讨论】:

    • 是的,我对使用 actionmailer 有点困惑,呵呵感谢链接,我已经检查了 railscasts 上的视频。谢谢。
    • unclaimedbaggage,您使用 text_field 的方式看起来不起作用,如果我更改有关控制器的一些设置并使用,我会收到此错误消息:'undefined method `text_field' for nil:NilClass'一个 text_field :tableelement 可以工作,但是你的 f.text_field 'fromemail' 我得到了这个错误。感谢您的帮助
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 2013-12-02
    • 2013-12-19
    • 2012-02-11
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2013-02-04
    相关资源
    最近更新 更多