我正在观看这个视频http://railscasts.com/episodes/258-token-fields-revised,我也成功地实现了这一点。但现在我使用命名空间。我有lends_controller在文件夹员工在资产文件夹。这是我的贷款控制器模型
class Employee::Asset::Lend
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :text, type: String
field :date
field :asset_tokens
field :user_id, type: String
has_and_belongs_to_many :assets
belongs_to :tags
def asset_tokens=(tokens)
self.asset_ids = Asset.ids_from_tokens(tokens)
end
end
现在我有另一个模型资产。这里我必须定义有和属于这个借出模型我还做了这个
class Asset
include Mongoid::Document
field :name, type: String
field :description, type: String
field :serial_number, type: String
field :status, type: Integer
field :tag_tokens
field :quantity, type: Integer
validates_presence_of :name
validates :serial_number,:uniqueness => true
has_and_belongs_to_many :employee_asset_lends
has_and_belongs_to_many :tags
def self.tokens(query)
assets = where(name: /#{Regexp.escape(query)}/i)
end
借阅控制器的形式是
<%= f.label :asset_tokens, "Assets" %>
<%= f.text_field :asset_tokens, data: {load: @employee_asset_lend.assets}%><br>
<%= f.input :date,:input_html => { :class => "dp1"},:label=> "Lend Date"%>
inside coffescript file for lend.js.coffee
jQuery ->
$('#employee_asset_lend_asset_tokens').tokenInput '/assets.json'
theme: 'facebook'
prePopulate: $('#employee_asset_lend_asset_tokens').data('load')
但它给出了错误uninitialized constant EmployeeAssetLend from asset views.
从借出视图,它给出错误像undefined method
all_of' for Employee::Asset:Module '
请查看创建HABTM-has_and_belongs_to_many关联的正确方法获取更多详细信息