Rails 邮件程序 +从模型发送邮件

发布于 2024-12-02 17:03:17 字数 1888 浏览 1 评论 0原文

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

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

我创建了一个生成器:

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)):

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

谢谢。

I have a model that has some attached files.
In the show action, I have a link to open a dialog that shows a form.

This form is for sending email with the attached files from the parent models, as well as some information about the model.

I created a generator:

rails g mailer notifier
rails g resource emailsystem

For now, i have a div in the show

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

But I don't know with what model i need to attach this form, because the form doesn't need a db.
I have created a new resource, but when I use it, I get an error because the resource table doesn't exist.

How I can do that? I think I need to attach the dialog form with the current model.

EDIT ------------

Now i have created my form and controller and mailer + models

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 %>

but with all that, i get this error :

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)):

Why i have wrong number of arguments? Where i can set the number of arguments for my models/controller?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2024-12-09 17:03:18

简短的答案是使用 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

The short answer is use form_tag instead of form_for. So:

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

The longer answer is you may want to read a tutorial or two on actionmailer - I suspect you might be a little confused about how mail works in rails(?) ;-)

Documentation here: http://api.rubyonrails.org/classes/ActionMailer/Base.html

or if you prefer, the RailsCast: http://railscasts.com/episodes/206-action-mailer-in-rails-3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文