0

Rails の searchlogic プラグインにはあまり詳しくありません (Railscast は確認しましたが、以下の特定のコードに関しては役に立ちませんでした)。以下の3つの方法でそれがどのように使用されているかを簡単に説明できる人はいますか? 返信ありがとうございます。

 def extract_order
@order_by = if params[:order].present?
  field = params[:order].gsub(".", "_")
  field = field.starts_with?('-') ? 'descend_by_'+field[1..-1] : 'ascend_by_'+field
  field.to_sym
else
  # Workaround
  'searchlogic'.to_sym
end
end

def find_resources
@search_conditions = params[:search_conditions] || {} # See http://www.binarylogic.com/2008/11/30/searchlogic-1-5-7-complex-searching-no-longer-a-problem/
@resources = @resource_model.send(@order_by).searchlogic(:conditions => @search_conditions) 
end

def apply_filters
f = filter_by
f.each do |filter_field|
  filter_constraints = params[filter_field.to_sym]
  if filter_constraints.present?
    # Apply searchlogic's scope
    @resources.send(filter_field,filter_constraints)
  end
end
end
4

1 に答える 1

0

メソッドapply_filterが呼び出されていません。

メソッドfind_resourcesは@order_byのコンテンツを使用しています(メソッドの抽出順序は呼び出されませんが)

したがって、リソースモデルでの検索は、変数検索条件に格納されているparams(おそらくユーザー入力)を使用し、@order_byを使用して使用する必要のある順序を指定して実行されます。

アプリケーションがいくつかのパラメータを取得し、「。」を変更していることに注意してください。"_"に変換し、部分文字列を取得します(1 ..-1、実際には最初の文字を削除し、スコープ検索(ascend_by_ | descend_by_)のパラメーターとして使用します)。
これはsearchlogicの機能であり、動的ファインダーとして使用できます。 :ascend_by_name_of_field。

IMO、散らかっています。@order_byが空ではなく、関数extract_orderがすでに実行されていると想定しています。もう1つ、ユーザーとの対話がないアクションにはアクセスできないようにする必要があります。

于 2010-05-06T20:51:01.163 に答える