I get the following error when I call ContactUser.new:
contact_user.rb:13: syntax error, unexpected ')', expecting '='
contact_user.rb:19: syntax error, unexpected kEND, expecting $end
Usually an error like this is pretty obvious, but I can't seem to pinpoint the cause. Any help would be appreciated.
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开发者_JAVA百科.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
Here's your problem:
def self.create_from_params(params={}, recipient)
#---------------------------------^^^
You're declaring a default value for the params
parameter to create_from_params
but you don't provide a default for recipient
. Parameters with defaults must appear at the end of the argument list and they can't be followed by parameters without defaults.
This error message:
contact_user.rb:13: syntax error, unexpected ')', expecting '='
tells us that Ruby was expecting to see an equals sign for the recipient
default value when it hit the closing parenthesis for the argument list.
精彩评论