问题
I'm following a tutorial referenced in a StackOverflow post for a simple contact form. Tutorial is here: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/
Everything is copy/paste and change a few values for my domain and mail settings. Running into two very strange errors: When I first submit the contact form, I get a :
stack level too deep
Rails.root: /Users/me/application
Application Trace | Framework Trace | Full Trace
This error occurred while loading the following files:
mail/parsers/rfc2822_obsolete
If I resubmit without restarting the rails app, I get:
uninitialized constant Mail::Ruby19
Rails.root: /Users/me/application
Application Trace | Framework Trace | Full Trace
app/mailers/notifications_mailer.rb:1:in `'
app/controllers/contact_controller.rb:11:in `create'
This error occurred while loading the following files:
The sections I feel might be relavent are:
class NotificationsMailer < ActionMailer::Base
default :from => "feedback@mydomain.com"
default :to => "me@gmail.com"
def new_message(message)
@message = message
mail(:subject => "mydomain Feedback")
end
end
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body
validates :name, :email, :subject, :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
class ContactController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
NotificationsMailer.deliver_new_message params[:message]
# NotificationsMailer.new_message(@message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
The rest of the templates are very straightforward and rendering fine.
Also, attaching the gemfile in case I missed some obvious conflict in there:
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'sqlite3'
gem 'redis', '2.1.1'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'jade'
gem "haml", ">= 3.1.7"
gem "haml-rails", ">= 0.3.5", :group => :development
gem "hpricot", ">= 0.8.6", :group => :development
gem "ruby_parser", ">= 2.3.1", :group => :development
gem "rspec-rails", ">= 2.11.4", :group => [:development, :test]
gem "email_spec", ">= 1.2.1", :group => :test
gem "cucumber-rails", ">= 1.3.0", :group => :test, :require => false
gem "database_cleaner", ">= 0.9.1", :group => :test
gem "launchy", ">= 2.1.2", :group => :test
gem "capybara", ">= 1.1.2", :group => :test
gem "factory_girl_rails", ">= 4.1.0", :group => [:development, :test]
gem "bootstrap-sass", ">= 2.1.0.1"
gem "devise", ">= 2.1.2"
gem "cancan", ">= 1.6.8"
gem "rolify", ">= 3.2.0"
gem "simple_form", ">= 2.0.4"
gem "quiet_assets", ">= 1.0.1", :group => :development
gem "hub", ">= 1.10.2", :require => nil, :group => [:development]
gem 'paperclip'
gem 'aws-sdk'
gem 'aws-s3'
gem "high_voltage"
Thank you in advance.
回答1:
I think the following code from ContactController.rb is the culprit:
NotificationsMailer.deliver_new_message params[:message]
You do not actually have a method name deliver_new_message
Try removing it, and uncommenting the line below it:
NotificationsMailer.new_message(@message).deliver
Hope this helps
来源:https://stackoverflow.com/questions/14051056/rails-3-2-8-actionmailer-stack-level-too-deep-plus-uninitialized-constant-mail