Rails 4一页应用程序联系表单

时间:2014-11-21 09:52:47

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个单页应用,底部有联系表单,但我的所有字段都有未定义的方法

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

表格欢迎#index.html.erb

   <%= 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

1 个答案:

答案 0 :(得分:1)

您的Contact模型实际上是否有spam属性?

通过查看您的迁移进行检查,或者启动Rails控制台并输入Contact,它将显示它所知道的属性。