访问该页面时收到错误消息:localhost:3000/contacts
错误,详细信息和源代码如下所示。
错误的参数数量(1对2)
提取的来源(第4行附近):
2 attribute :name, :validate => true
3 attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
4 attribute :message
5 attribute :nickname, :captcha => true
model.rb
class Contact < ApplicationRecord
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
attribute :nickname, :captcha => true
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "HsbNoid",
:to => "iamhsb001@gmail.com",
:from => %("#{name}" <#{email}>)
}
end
end
控制器代码
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(conact_params)
@contact.request = request
if @contact.deliver
flash.now[:notice] = 'Thank you for your message. We will contact you soon!'
else
flash.now[:error] = 'Cannot send message.'
render :new
end
end
private
def conact_params
params.require(:contact).permit(:name, :email, :message)
end
end
答案 0 :(得分:1)
在model.rb
第4行,您忘了添加&#39;,&#39;。这应该是attribute :message,