【问题标题】:ArgumentError in ContactsController#newContactsController 中的参数错误#new
【发布时间】:2016-07-31 06:39:18
【问题描述】:

访问页面时出现错误: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

【问题讨论】:

  • 你得到了什么错误?请发布完整的堆栈跟踪。
  • 参数数量错误(1 对 2)
  • “参数数量错误(1 对 2)”——绝不是堆栈跟踪。

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


【解决方案1】:

model.rb 第 4 行中,您忘记添加“,”。这应该是attribute :message,

【讨论】:

  • 现在我在 ContactsController#new 这一行 "attribute :nickname, :captcha => true" 中得到 SyntaxError,错误是语法错误,意外 tSYMBEG,期待 keyword_do 或 '{' 或 '('属性 :nickname, :captcha => true ^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 2013-06-10
相关资源
最近更新 更多