我有一个单页应用,底部有联系表单,但我的所有字段都有未定义的方法
undefined method `spam' for #<Contact:0x007fe9e4471ff0>
def index
@contact = Contact.new
end
def submit
redirect_to root_path and return if params[:spam].present?
@contact = Contact.new(contact_params)
if @contact.valid?
ContactFormMailer.admin(@contact).deliver
redirect_to root_url
flash[:success] = "Message sent! Thank you for contacting us"
else
redirect_to(:action => 'index')
end
end
private
def contact_params
params.require(:contact).permit(:spam, :first_name, :last_name, :email, :phone, :message)
end
<%= simple_form_for :contact_form, url: contact_form_path, method: :post do |f| %>
<%= f.input :spam, as: :hidden %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :phone %>
<%= f.input :message, as: :text %>
<%= f.submit 'Submit', class: "button small" %>
<% end %>
class Contact < ActiveRecord::Base
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true, :format => { :with => %r{.+@.+\..+} }
validates :message, presence: true
end
match '/contact' => 'welcome#index', :via => :get
match '/contact' => 'welcome#submit', :via => :post
答案 0 :(得分:1)
您的Contact
模型实际上是否有spam
属性?
通过查看您的迁移进行检查,或者启动Rails控制台并输入Contact
,它将显示它所知道的属性。