Ruby on rails 尝试使用mailer rails 4实现联系我们表单时出现NoMethodError
我对rails相对来说比较陌生,我在很多事情上都在绞尽脑汁。但现在主要是ActionMailer。我正在尝试建立一个联系我们的表单 我现在收到这个错误Ruby on rails 尝试使用mailer rails 4实现联系我们表单时出现NoMethodError,ruby-on-rails,actionmailer,nomethoderror,Ruby On Rails,Actionmailer,Nomethoderror,我对rails相对来说比较陌生,我在很多事情上都在绞尽脑汁。但现在主要是ActionMailer。我正在尝试建立一个联系我们的表单 我现在收到这个错误 NoMethodError in ContactsController#new private method `new' called for Contact:Class def new @contact = Contact.new end 这是我的邮箱/contact.rb 1 class Contact < Act
NoMethodError in ContactsController#new
private method `new' called for Contact:Class
def new
@contact = Contact.new
end
这是我的邮箱/contact.rb
1 class Contact < ActionMailer::Base
2 default from: "my@email.com"
3
4
5 def contact_us(subject)
6 mail(to: 'my@email.com', subject: subject)
7 end
8
9 end
1类联系人
这是我的联系人。\u controller.rb
1 class ContactsController < ApplicationController
2 def new
3 @contact = Contact.new
4 end
5
6 def create
7 @contact = Contact.new(contact_params)
8 #Tell the contact model to send contact_us
9 if Contact.contact_us(subject).deliver
10 flash.now[:error] = nil
11 flash.now[:notice] = 'Thank you for your message!'
12 else
13 flash.now[:error] = 'Cannot send message.'
14 render :new
15 end
16 end
17 private
18 def contact_params
19 params.require(:contact).permit(:name, :email, :message)
20 end
21 end
1类联系人控制器
最后是观点
1 <%= form_for @contact do |f| %>
2 <p>
3 <%=f.input :name %>
4 </p>
5 <p>
6 <%=f.input :email %>
7 </p>
8 <p>
9 <%=f.text_area :message %>
10 </p>
11 <p>
12 <%= f.submit %>
13 </p>
14 <%end%>
15
1
2
3.
四,
5
6.
七,
8
9
十,
11
12
十三,
14
15
我知道解决方案可能就在眼前。但如果可能的话,我真的非常感谢你的帮助。
如果我还可以为您提供什么,请告诉我。您的代码中有一些不同的内容。首先,如果您从
ActionMailer::Base
继承,则创建的是一个Mailer类,而不是ActiveRecord模型。我会将该类重命名为ContactMailer,或者只对整个应用程序使用一个Mailer
,称之为Mailer
这也是您无法呼叫Contact.new的原因。如果您的Contact类继承自ActiveRecord::Base
,则可以在模型上调用new
,并实例化该对象的实例。但是,对于mailers,您正在定义类方法,每个类方法都被定义为发送特定的电子邮件。您永远不需要创建从ActionMailer继承的对象的新实例
假设您有一个类Mailer
,在该类中您有一个方法def contact\u us(subject)
,您可以调用:
Mailer.contact_us(subject).deliver
发送电子邮件