gpt4 book ai didi

ruby-on-rails-3 - Ransack gem 选择谓词和自定义谓词名称

转载 作者:行者123 更新时间:2023-12-04 02:44:01 24 4
gpt4 key购买 nike

我对使用高级搜查搜索有点困惑。我正在尝试进行自定义搜索,其中并非所有表名都可以被选为搜索词,并且并非所有谓词都被使用。我用过 railscast作为本教程,但我找不到任何如何限制谓词数量的方法。是否还有可能使用不同语言(只是标签)的谓词和表字段的名称?

我的搜索表单

= search_form_for @q, :url => search_offers_path, :html => { :method => :post } do |f|
= f.condition_fields do |c|
.field
= f.attribute_fields do |a|
= a.attribute_select
= f.predicate_select
= f.value_fields do |v|
= v.text_field :value
= link_to "#{t :destroy}", '#', class: "remove_fields"

= link_to_add_fields "#{t :add}", f, :condition

.field
= t :sort
= f.sort_fields do |s|
= s.sort_select

= f.submit "#{t :search}"

我的 Controller
def index
select_offers = Offer.where { (user_id != id) & (ended == false) & ((created_at + life_time ) > DateTime.now) }
@q = select_offers.search(params[:q])
@offers = @q.result(:distinct => true).page(params[:page])
@q.build_condition
@q.build_sort if @q.sorts.empty?
end

最佳答案

我找到了解决办法。
为了更改谓词标签,我使用了 i18n。

en.yml

ransack:
asc: "ascending"
desc: "descending"
predicates:
cont: "contains"
not_cont: "not contains"
start: "starts with"
end: "ends with"
gt: "greater than"
lt: "less than"
...

还可以更改属性字段的名称
attributes:
model_name:
model_field1: "field name1"
model_field2: "field name2"
...

限制搜索谓词而不是
= f.predicate_select 

我用了
= f.attribute_fields do |a|
= a.attribute_select
= f.select :p, { 'custom predicate name1' => :predicate1, 'custom predicate name2' => :predicate2 }

为了限制我添加到模型中的表搜索字段
UNRANSACKABLE_ATTRIBUTES = ["id", "published", "created_at"]

def self.ransackable_attributes auth_object = nil
(column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys
end

关于ruby-on-rails-3 - Ransack gem 选择谓词和自定义谓词名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12528969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com