如何将ransack param [:q]对象传递给rails中的另一个方法

时间:2015-03-27 07:00:39

标签: ruby-on-rails ruby-on-rails-3 ransack

我正在尝试将我的ransack params [:q]对象传递给我的演示者类但收到No Ransack::Search object was provided to search_form_for!错误。

这是我的示例代码

client_managers_widgets / index.html.haml

= search_form_for @search, :url => admin_client_managers_widgets_path do |f|
  = render :partial => "admin/share/filter_wrapper", :locals => { :f => f, :filter=> "admin/client_managers_widgets/filters/redemption_filter" }

_redemption_filter.html.haml

%table.filters
  %tr
    %td
      = f.label :created_at_gteq, "Select Period"
      = f.select :created_at_gteq, { "Start Till Date" => "", "This Year" => Date.today.beginning_of_year, "Last Month" => 1.month.ago.beginning_of_month, "This Month" => Date.today.beginning_of_month }
    %td
      = f.label :user_telecom_circle_regional_managers_id_eq, "Region"
      = f.collection_select :user_telecom_circle_regional_managers_id_eq, @telecom_regions.select_options, :id, :region, {:include_blank => t(:all)}, :class => 'telecom_circle_regional_managers_id_eq'
  %tr
    %td
      = f.label :created_at_date_gteq,"Select Custome Time"
      = f.text_field :created_at_date_gteq, :class => 'datepicker input-small'
      = f.label "-", :class=>"to"
      = f.text_field :created_at_date_lteq, :class => 'datepicker input-small'

controller => client_managers_widgets

def index
     @client_manager_dashboard = ClientManagerDashboard.new(current_admin_user, search_params)
     @users = @client_manager_dashboard.get users
   # .
  # .
  # .
end

private

def search_params
  params[:q]
end

presenter => client_manager_dashboard

class ClientManagerDashboard
  def initialize(admin_user, search_params)
    @current_ability = Ability.new(admin_user)
    @search_params = search_params
  end

  def get_users
    @search = User.joins(:client).where(clients: {id: clients}).search(search_params)
    @users = @search.result
  end
  private

  def current_ability
    @current_ability
  end

  def search_params
    @search_params
  end
end

现在我刚刚添加了基本代码片段,但这是一般化的场景。 请让我知道任何人已经实现了这种情况。

2 个答案:

答案 0 :(得分:2)

你需要一个法术Flat Scope,就像这样

def define_methods
 shared = 0
 Kernel.send :define_method, :counter do
  shared
 end
 Kernel.send :define_method, :inc do |x|
  shared += x
 end
end

你可以阅读“Metaprogramming Ruby”

答案 1 :(得分:1)

在我看来,@search变量在此代码中为零:

= search_form_for @search, :url => admin_client_managers_widgets_path do 
|f|

因为@searchClientManagerDashboard的实例变量。例如,这应该有效:

class ClientManagerDashboard
  .. 
  def get_search
    @search = User.joins(:client).where(clients: {id: clients}).search(search_params)
  end
  ..
end

在你看来:

= search_form_for @client_manager_dashboard.get_search, :url => admin_client_managers_widgets_path do |f|