2012-12-26 150 views
4

我遇到了交貨問題。Rails 3 - 未定義的方法`delivery_handler ='

我得到正是這個錯誤:

NoMethodError in PagesController#send_message 
undefined method `delivery_handler=' for #< Message:0x00000004db9648> 

app/mailers/contact.rb:22:in `send_email' 
app/controllers/pages_controller.rb:65:in `send_message' 

總之,這裏是我的代碼:

/views/pages/contact.html.erb

<h1><%= @page.title %></h1> 
<%= @page.content.html_safe %> 

    <%= form_for @message, :url => "/pages/send_message" do |f| %> 
    <% if flash[:notice] %> 
     <% flash[:notice].full_messages.each do |msg| %> 
      <p><%= msg %></p> 
     <% end %> 
    <% end %> 
    <div class="field"> 
     <%= f.label :email %> 
     <%= f.text_field :email %> 
    </div> 
    <div class="field"> 
     <%= f.label :body %> 
     <%= f.text_area :body %> 
    </div> 
    <%= f.submit "Envoyer" %> 
    <% end %> 

/模型/ message.rb

 class Message 
     include ActiveModel::Validations 
     include ActiveModel::Conversion 
     extend ActiveModel::Naming 

     attr_accessor :email, :body 

     validates :email,:body, :presence => true 
     validates :email, :format => { :with => %r{[email protected]+\..+} }, :allow_blank => true 

     def initialize(attributes = {}) 
     attributes.each do |name, value| 
      send("#{name}=", value) 
     end 
     end 

     def persisted? 
     false 
     end 
    end 

* /控制器/ pages_controller.rb *

def send_message 
     @message = Message.new(params[:message]) 

     if @message.valid? 
     Contact.send_email(@message).deliver 
     redirect_to request.referer, :notice => "Le message a &eacute;t&eacute; envoy&eacute;." 
     else 
     redirect_to request.referer, :notice => "Vous n'avez pas entrer votre email." 
     end 
    end 

/app/mailers/contact.rb

 class Contact < ActionMailer::Base 
     attr_accessor :email, :message 

     default to: "[email protected]" 
     default from: "[email protected]" 

     def send_email(message) 
      @message = message 
      mail(:subject => "Nouveau contact blo") 
     end 
     end 

/config/environments/development.rb

config.action_mailer.default_url_options = { 
     host: 'localhost:3000' 
    } 
    config.action_mailer.perform_deliveries = true 
    config.action_mailer.delivery_method = :sendmail 
    config.action_mailer.sendmail_settings = { 
     location: '/usr/sbin/sendmail', 
     arguments: "-i -t -f [email protected]" 
    } 

回答

9

在您的郵箱中,取出attr_accessor :message或稱之爲別的東西。

ActionMailer::Base已經有一個消息訪問器獲取/設置正在構建的Mail::Message,所以軌位呼籲message期望得到一個Mail::Message而是讓你的郵件類代替。

+0

謝謝,我在我的消息後的晚上找到了解決方案。但是,你的答案完全確切。因爲我沒有足夠的聲望,所以無法贊成,但如果可以的話,我會盡力。非常感謝你。 –

+1

upvoted for you :) –

相關問題