安装了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:
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
我觉得你应该重新安装acts_as_taggable插件,毕竟它和acts_as_taggable_on_steroids插件有不少不同的地方。http://thegiive.iteye.com/blog/75716介绍如何使用插件acts_as_taggable。
[code="ruby"]ruby script/generate acts_as_taggable_migration
rake db:migrate
[/code]
这个执行了没,是不是缺少tag表呢!
[code="ruby"] def tag_with(tags) (bookmark.rb第27行)
Tag.transaction do
taggings.destroy_all
[color=red] tags.each { |name| Tag.find_or_create_by_name(name).on(self)[/color] }
end
end [/code]
.on(self)这里有问题,不知道你为什么不用插件自带的方法,非要自己写,这样和不用有什么区别?
下面方法不能满足你上面自己写的方法吗?
.tag_list.include?('warning')
.tag_list.remove("warning")
.tag_list.add("Great", "Awful")
我觉得这个插件对你没多大用,因为你就没用它的方法,还有find_or_create_by_name这个方法也不是这个插件自带的,觉得你用起来好复杂呀。还是你的要求复杂?