安装了acts_as_taggable_on_steroids插件(ruby 1.8.6, rails 2.0.2),其中模型代码bookmark.rb包括:
def tag_with(tags) (bookmark.rb第27行)
Tag.transaction do
taggings.destroy_all
tags.each { |name| Tag.find_or_create_by_name(name).on(self) }
end
end
程序主要实现为网页添加标签,在运行时发生错误,其中log记录如下:
####################################################################################################
NoMethodError (undefined method on' for #<Tag id: 16, name: "good,god">):
method_missing'
C:/INSTAN~1.0-W/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:205:in
/app/models/bookmark.rb:29:in tag_with'
each'
/app/models/bookmark.rb:29:in
/app/models/bookmark.rb:29:in tag_with'
transaction'
C:/INSTAN~1.0-W/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in
C:/INSTAN~1.0-W/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:80:in transaction'
tag_with'
/app/models/bookmark.rb:27:in
/app/controllers/bookmarks_controller.rb:40:in `create'
####################################################################################################
在控制器bookmark_controller.rb中调用方法tag_with:
POST /users/{username}/bookmarks
def create
bookmark = Bookmark.find_by_user_id_and_uri(params[:bookmark][:user_id],
params[:bookmark][:uri])
if bookmark
# This user has already bookmarked this URI. They should be
# using PUT instead.
headers['Location'] = bookmark_url(@user.name, bookmark.uri)
render :nothing => true, :status => "409 Conflict"
else
# Enforce default values for 'timestamp' and 'public'
params[:bookmark][:timestamp] ||= Time.now
params[:bookmark][:public] ||= "1"
# Create the bookmark in the database.
bookmark = Bookmark.new(params[:bookmark])
if bookmark.save
# Add tags.
bookmark.tag_with(params[:taglist]) if params[:taglist] (调用方法tag_with)
# Send a 201 response code that points to the location of the
# new bookmark.
headers['Location'] = bookmark_url(@user.name, bookmark.uri)
render :nothing => true, :status => "201 Created"
else
render :xml => bookmark.errors.to_xml, :status => "400 Bad Request"
end
end
end