Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - undefined method `delivery_handler='

I have a problem with delivery.

I get exactly this error:

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'

Anyway, here's my code:

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

/models/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{.+@.+\..+} }, :allow_blank => true

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

       def persisted?
         false
       end
     end

*/controllers/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]"
    }
like image 314
Louis XIV Avatar asked Jan 14 '23 16:01

Louis XIV


1 Answers

In your mailer, remove the attr_accessor :message or call it something else.

ActionMailer::Base already has a message accessor that gets/sets the Mail::Message being built, so bits of rails are calling message expecting to get a Mail::Message but is getting your Message class instead.

like image 162
Frederick Cheung Avatar answered Jan 21 '23 11:01

Frederick Cheung