无表格模型的语法错误

时间:2011-07-16 08:06:42

标签: ruby-on-rails ruby

当我致电ContactUser.new时,我收到以下错误:

contact_user.rb:13: syntax error, unexpected ')', expecting '='
contact_user.rb:19: syntax error, unexpected kEND, expecting $end

通常这样的错误很明显,但我似乎无法确定原因。任何帮助将不胜感激。

class ContactUser < ActiveRecord::Base

  include Tableless

  column :name, :string
  column :email, :string
  column :category, :string
  column :message, :text
  column :recipient, :string

  validates_presence_of :name, :email, :category, :message, :recipient

  def self.create_from_params(params={}, recipient)
    params[:message].encode!('US-ASCII', :undef => :replace) # re-encode message in US ASCII to ensure mailer works with it

    ContactUser.create(:name => params[:name], :email => params[:email], :category => params[:category], :message => params[:message], :recipient => recipient)
  end

end

module Tableless

  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods

    def columns() @columns ||= []; end

    def column(name, sql_type=nil, default=nil, null=true)
      columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
    end

  end

  def save(validate=true)
    validate ? valid? : true
  end

end

1 个答案:

答案 0 :(得分:4)

这是你的问题:

def self.create_from_params(params={}, recipient)
#---------------------------------^^^

您要将params参数的默认值声明为create_from_params,但您没有为recipient提供默认值。默认值的参数必须出现在参数列表的末尾,并且没有默认值的参数不能跟随它们。

此错误消息:

contact_user.rb:13: syntax error, unexpected ')', expecting '='

告诉我们Ruby在命中参数列表的右括号时期望看到recipient默认值的等号。