2011-07-06 1 views
0

今は私の作者モデルと私の投稿モデルで、has_many through:authorshipsを使ってjqueryトークンフィールド(jquery_token_input gemを使用)言い換えれば、著者の名前を入力し始めると、その名前が表示され、それを選択します。結果が得られない場合は、代わりに[Add:new_name]を選択すると、新しい著者が作成されます。Rails 3:トークンフィールドは、私のコードを微調整するのに少し助けが必要です

私はこの同じ方法を使用して、ユーザーが「コレクション」の名前を割り当てるのと同じようにしたいと思いますが、1人のユーザーのコレクションのみが検索および表示されるように調整します。

私がcreateをコールすると、コレクションモデルにuser_id coloumnを割り当てる必要がありますか。どのように私はcurrent_userのuser_idも含めるように、 "%like {"、 "%#{params [:q}}%"という名前を変更しますか?

洞察やアイデアを本当にありがとうございます。

## 

class ArtistsController < ApplicationController 
    def index 
    @artists = Artist.where("name ilike ?", "%#{params[:q]}%") 
    results = @artists.map(&:attributes) 
    results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"} 

    respond_to do |format| 
     format.html 
     format.json { render :json => results } 
    end 
    end 


## Post model 

    has_many :artistizations 
    has_many :artists, :through => :artistizations 
    attr_reader :artist_tokens 

    def artist_tokens=(ids) 
    ids.gsub!(/CREATE_(.+?)_END/) do 
     Artist.create!(:name => $1).id 
    end 
    self.artist_ids = ids.split(",") 
    end 

答えて

0

私はこれも苦労しています。私はhabtm thoでこれをやろうとしています。 あなたのコードをチェックしてください:

@artists = Artist.where("name ilike ?", "%#{params[:q]}%") 

ilike - これはlikeすべきですか?

ああ、ilikeはPostgres用です。

関連する問題