2016-09-28 55 views
1

我有一個簡單的表,其中顯示所有用戶及其屬性id,名稱,年齡,狀態,位置。我想過濾年齡和地位。查看應用過濾器後不更新[紅寶石軌道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後」 -jquery「(早些時候它在它之前)
  • 完成上述操作後,過濾或排序仍然只在瀏覽器中刷新時發生,而我沒有找到解決方案,所以我寧願添加一個表單提交按鈕,如文檔(http://filterrific.clearcove.ca/pages/action_view_api.html)中的「禁用AJAX自動錶單提交」中所述。這樣你仍然應該點擊一個按鈕或按回車鍵,但至少過濾/排序工作。

還有一件事:當我想根據另一個表中的字段進行排序時(按照belongs_to外鍵關係),我也遇到了問題。如果我做到了如在GitHub的示例應用程序(https://github.com/jhund/filterrific_demo/blob/master/app/models/student.rb)的源極提到:

「命令(」 LOWER(countries.name)#{方向} 「)。包含(:國)」

我有一個SQL錯誤。當我把它改成像

「加入(:國家).order(」 LOWER(countries.name)#{}方向 「)」

它解決了這個問題。