错误的参数数量联系#create

时间:2016-10-11 10:20:59

标签: ruby-on-rails ruby

我有这个错误:

  

Contact#create中的ArgumentError

     

错误的参数数量(给定0,预期2)

它从我的视图中显示了这一行

  <%= @contact.errors.full_message.each do |message| %>

我在contact_controller中的create方法不带参数

contact_controller.rb

  def index
    @contact = Contact.all
  end

  def create
    @contact = Contact.new(contact_params)
    if @contact.save
      flash[:notice] = "Welcome"
      redirect_to "/"
    else
      flash[:alert] = "You filled wrong the form"
      render 'connection'
    end
  end

我应该从我的模型中显示那些消息

contact.rb

validates :firstname, presence: true, length: { maximum: 30, minimum:2,
    too_long: "30 characters is the maximum allowed.", too_short: "2 characters is the minimum allowed." }

validates :lastname, presence: true, length: { maximum: 50, minimum:2,
    too_long: "50 characters is the maximum allowed.", too_short: "2 characters is the minimum allowed." }

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, uniqueness: {message: "Email is already subscribed"}, length: { maximum: 40, minimum:7,
    too_long: "40 characters is the maximum allowed.", too_short: "7 characters is the minimum allowed.",},
    format: { with: VALID_EMAIL_REGEX }

validates :password, presence: true, length: { maximum: 30, minimum:6,
    too_long: "30 characters is the maximum allowed.", too_short: "6 characters is the minimum allowed." }

我不明白ruby想要的是什么。有谁知道它是什么?

1 个答案:

答案 0 :(得分:2)

您应该使用full_messages而不是full_message

full_message是一种获取密钥和缩写消息并将它们组合在一起的方法,因此它需要两个参数...

@contact.errors.full_message(:sausage, 'is too dry') 
=> "Sausage is too dry"