应用滤镜后视图未更新[rule rails filterrific] [英] view is not updating after applying filter [ruby rails filterrific]
问题描述
我有一个简单的表,其中显示了所有用户及其属性ID,名称,年龄,状态,位置.我想过滤年龄和状态.
I have a simple table which displays all the users and its attributes id,name,age,status,location. I want to filter on age and status.
Id name age status location
1 xz 22 single ca
2 yy 23 married ma
我正在使用 filterrific 插件,并且能够显示列表和过滤器下拉列表.
I am using filterrific plugin and able to display the list and the filter dropdown.
我的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看起来像
the index.html.erb looks like
<%= 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 }
) %>
当我转到页面时,我可以看到所有用户.现在,当我过滤时什么也没有发生.我仍然看到所有用户.不知道发生了什么.我感到我的范围过滤器未得到应用.
When I go to the page I am able to see all the users. Now when I filter nothing happens. I still see all the users. Not sure what is happening. I have feeling that my scope filter is not getting applied.
推荐答案
对我来说,这就是窍门:
To me this did the trick:
- 在app/assets/javascripts/application.js中,我在"//= require filterrific/filterrific-jquery"之后放了一行"//= require turbolinks"(之前是)
- 完成上述操作后,仅当我在浏览器中单击刷新"时,过滤或排序仍然发生,而我没有找到解决方案,因此我添加了一个表单提交按钮,如禁用AJAX自动表单提交"中所述"(在文档中( http://filterrific.clearcove.ca/pages/action_view_api.html).这样,您仍应单击按钮或按Enter键,但至少可以进行过滤/排序.
- In app/assets/javascripts/application.js, I've put the line "//= require turbolinks" after "//= require filterrific/filterrific-jquery" (earlier it was before it)
- After doing the above, filtering or sorting happened still only when I hit refresh in the browser, and I didn't find a solution to that, so I rather added a form submit button as mentioned in "Disable AJAX auto form submits" in the documentation (http://filterrific.clearcove.ca/pages/action_view_api.html). This way you should still click a button or hit Enter, but at least filtering / sorting works.
另一件事:我想按另一个表中的字段排序时也遇到了问题(跟从属关系到外键关系).如果我按照github(
One more thing: I had also a problem when wanting to sort according to a field in another table (following a belongs_to foreign key relationship). If I did it as mentioned in the source of the sample app in github (https://github.com/jhund/filterrific_demo/blob/master/app/models/student.rb):
"order(" LOWER(countries.name)#{方向}).includes(:country)"
"order("LOWER(countries.name) #{ direction }").includes(:country)"
我收到一个SQL错误.当我将其更改为类似
I got an SQL error. When I have changed it to something like
"joins(:country).order(" LOWER(countries.name)#{方向})"
"joins(:country).order("LOWER(countries.name) #{ direction }")"
它解决了这个问题.
这篇关于应用滤镜后视图未更新[rule rails filterrific]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!