0
我遇到文章問題標籤,用戶在登錄後可以編寫文章,並將標籤添加到文章底部......非常標準的東西。Rails 3 - 收拾標籤?
上午使用.titleised方法工作正常,但我注意到,如果用戶輸入3個標籤沒有逗號,然後顯示所有這些單詞作爲一個標籤。
有沒有一種方法,我可以自動輸入一個逗號後,每個單詞'提交'之前,他們是通過模型輸入數據庫,即使用戶不?
例如:紅,熱,屋頂,而不是紅熱屋頂
型號/ article.rb
def assign_tags
if @tag_names
self.tags = @tag_names.split(/\,/).map do |name|
Tag.find_or_create_by_name(name.titleize)
型號/ tag.rb
class Tag < ActiveRecord::Base
attr_accessible :name
validates :name, :uniqueness => true
# order by creation
default_scope :order => 'created_at DESC'
has_many :taggings, :dependent => :destroy
has_many :articles, :through => :taggings
end