[英]Rails auto_complete with a subset of objects
這是我的模特:
class User < ActiveRecord::Base
has_many :artists
end
class Artist < ActiveRecord::Base
belongs_to :user
end
我正在嘗試實現自動完成藝術家姓名的auto_complete文本字段:
<%= text_field_with_auto_complete :artist, :name, { :size => 60}, {:skip_style => false, :tokens => ','} %>
這可以工作,但會自動填充數據庫中定義的所有藝術家。 我需要做什么才能將返回的auto_complete結果限制為僅屬於登錄用戶的藝術家?
非常感謝!
想必你在你的控制器中有這樣的東西:
def auto_complete_for_artist_name
@artists = Artist.find(:all,
:conditions => "name LIKE (?)", params[:artist][:name])
end
您需要進行更改,以將當前用戶添加到條件中,或使用如下所示的關聯:
def auto_complete_for_artist_name
# assumes you have a 'current_user' method
# which returns the current logged in user
@artists = current_user.artists.find(:all,
:conditions => "name LIKE (?)", params[:artist][:name])
end
這將只給您屬於當前用戶的藝術家。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.