2016-09-28 7 views
1

私はすべてのユーザとその属性id、name、age、status、locationを表示するシンプルなテーブルを持っています。私は年齢と状態をフィルタリングしたいフィルタを適用した後にビューが更新されない[ruby rails filterrific]

Id name age status location 
1 xz 22 single ca 
2 yy 23 married ma 

私はfilterrificプラグインを使用して、リストとフィルタードロップダウンを表示することができています。

マイuser.rb

filterrific(
    available_filters: [ 
     :with_status, 
     :with_age 
    ] 
) 

    scope :with_age, lambda { |age| 
    where(age: [*age]) 
    } 

    scope :with_status, lambda { |status| 
    where(status: [*status]) 
    } 


    def self.options_for_select 
    order('LOWER(status)').map { |e| [e.status] }.uniq 
    end 
    def self.options_for_select2 
    order('LOWER(age)').map { |e| [e.age] }.uniq 
    end 

コントローラインデックスは

def index 
    @filterrific = initialize_filterrific(
     User, 
     params[:filterrific], 
     select_options: { 
     with_status: User.options_for_select, 
     with_clearance_batch_id: User.options_for_select2 
     }, 
     default_filter_params: {}, 
     available_filters: [], 
    ) or return 

    @users = @filterrific.find 
    respond_to do |format| 
     format.html 
     format.js 
    end 

    end 

のように見えます私は私がページに移動するとindex.html.erbは

<%= form_for_filterrific @filterrific do |f| %> 

    <div> 
    age 
    <%= f.select(
     :with_age, 
     @filterrific.select_options[:with_age], 
     { include_blank: '- Any -' }, 
     {class: 'form-control' } 
    ) %> 
    </div> 
    <div> 
    status 
    <%= f.select(
     :with_status, 
     @filterrific.select_options[:with_status], 
     { include_blank: '- Any -' }, 
    ) %> 
    </div> 

<% end %> 

<%= render(
    partial: 'browse_users/list', 
    locals: { users: @users } 
) %> 

のように見えますすべてのユーザーを見ることができます。今私は何もフィルターをかけることはありません。私はまだすべてのユーザーを参照してください。何が起こっているのか分かりません。スコープフィルタが適用されていないと感じています。私に

答えて

0

これはトリックでした:アプリ/資産/ JavaScriptの/ application.jsで

  • を、私はラインを入れている "// = turbolinksを必要とする" //は= filterrific/filterrificを要求」した後、私がブラウザでリフレッシュしたときにのみフィルタリングや並べ替えが起こってしまったので、解決策が見つからなかったので、代わりにフォーム送信を追加しました。 (http://filterrific.clearcove.ca/pages/action_view_api.html)の「AJAX自動フォーム送信を無効にする」で説明したボタンをクリックします。あなたはまだボタンをクリックするかEnterキーを押すべきですが、少なくともフィルタリング/並べ替えは機能します。

もう1つ:別のテーブルのフィールドに従ってソートしたいときにも問題がありました(belongs_to外部キーの関係に従います)。 githubのサンプルアプリ(https://github.com/jhund/filterrific_demo/blob/master/app/models/student.rb)のソースで述べたように、私はそれをした場合:

"注文(" LOWER(countries.name)#{方向} ")。(国)を含みます" SQLエラーが発生しました。私は

のようなものに "(:国)加入.ORDERそれを変更した場合(" LOWER(countries.name)#{方向} ")"

をそれが問題を解決しました。

関連する問題