开发者

Rails mailer + sendmail from a models

开发者 https://www.devze.com 2023-04-01 20:47 出处:网络
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.

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消