Question

I cannot figure out this issue, even if it does not seem complicated...

I want to make a form to send emails in my rails app but this one does not work. I receive the following error:

TypeError in MessagesController#create: #<Message content: "test", email: "test@test.fr", name: "test"> is not a symbol

Error occurs in: app/controllers/messages_controller.rb:10:in 'create'

1.here is my messages_controller:

class MessagesController < ApplicationController

  def new
    @message = Message.new
  end

  def create
    @message = Message.new(params[:message])
    if @message.valid?
      Messenger.send(@message).deliver
      redirect_to root_url, notice: "Message sent! Thank you for contacting us."
    else
      render "new"
    end
  end

end
  1. the form new.html.erb:

    <%= form_for @message do |f| %>
        <%= f.text_field :name %>
        <%= f.text_field :email %>
        <%= f.text_area :content, :rows => 5 %>
    <%= f.submit "Send Message" %>
    <% end %>
    
  2. the Message model built using ActiveAttr:

    class Message
      include ActiveAttr::Model
    
      attribute :name
      attribute :email
      attribute :content
    end
    
  3. My Messenger mailer:

    class Messenger < ActionMailer::Base
      default :from => "test@test.com"
    
      def send(message)
        mail(:to => "test@test.com", :subject => "test")
      end
    
    end
    

Thank you for your help!

Was it helpful?

Solution

replace send with a different method name because it conflicts with the one defined in ruby.

http://ruby-doc.org/core-2.0/Object.html#method-i-send

OTHER TIPS

I had a similar TypeError using the send method, because it was expecting a symbol and I passed in something else. I fixed it by doing something like

send(some_variable) if some_variable == Symbol

However, it looks like you're trying to send mail and you're using an old way to do it. Rails 4.1.8 has a new way to do so here: http://api.rubyonrails.org/classes/ActionMailer/Base.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top