Rails 3联系表,未定义的方法? [英] Rails 3 Contact Form, undefined method?
问题描述
我正在使用以下Railscast在Rails 3中建立联系表单: http:// railscasts.com/episodes/326-activeattr?view=asciicast
I'm building a contact form in Rails 3 using this Railscast: http://railscasts.com/episodes/326-activeattr?view=asciicast
我知道我唯一做错的就是填写消息应该在哪里被发送。我使用以下代码发送消息:
I know that the only thing I'm doing wrong is filling out where the message should be sent. I used the following code to send the message:
mail(:to => "me@myemail.com")
但是,这似乎不起作用,因为每次我提交表单时,都会出现此错误:
However, this doesn't seem to work, because everytime I submit the form, I get this error:
NoMethodError in MessagesController#create
undefined method `mail' for #<MessagesController:0x00000103734bd8>
Application Trace | Framework Trace | Full Trace
app/controllers/messages_controller.rb:10:in `create'
我应该用此行替换为发送消息吗?
What should I replace this line with to send the message?
messages_controller.rb
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
UserMailer.contact_message(@message).deliver
redirect_to root_url, notice: "Message sent! Thank you for contacting us."
else
render "new"
end
end
end
user_mailer.rb
class UserMailer < ActionMailer::Base
def contact_message(message)
@message = message
mail(:to => "myemail@mymail.com", :subject => "New Message")
end
end
setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mywebsite.com",
:user_name => "myemail",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
推荐答案
我在这里回答了一个类似的问题: https:/ /stackoverflow.com/a/17883328/307308
I just answered a similar question here: https://stackoverflow.com/a/17883328/307308
您丢失了 from
属性
mail(:from => 'system@mymail.com', :to => "myemail@mymail.com", :subject => "New Message")
这篇关于Rails 3联系表,未定义的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!