Ransack搜索表单中的动态路径

时间:2014-05-08 12:36:39

标签: ruby-on-rails ransack

我正在使用Ransack搜索我的搜索表单。搜索表单如下所示:

.table-actions
  .search
    = search_form_for [:admin, search] do |f|
      .field
        = f.text_field :name_cont, placeholder: I18n.t(".general.search")
      .actions= f.submit I18n.t(".general.search_button")
  .add-record
    %button= link_to I18n.t(".general.table.add"), new_admin_plan_path

现在我想将此搜索表单移动到部分页面,因此可以在多个页面上重用,有些页面在命名空间admin内,有些则不在。 所以我创建了search_form_for看起来像这样的部分:

= search_form_for path do |f|

我想这样称呼它:

= render "shared/table_actions", search: @search, path: ??

路径应该具有哪些动态值才能使其发挥作用?或者有更好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

可能已经晚了,但这是可行的解决方案:

假设我们在clients_controller中有2个操作:

  def index
    @ransack_clients = Client.ransack(params[:clients_search], search_key: :clients_search)
    @clients = @ransack_clients.result.paginate(:page => params[:page], :per_page => 15).order("created_at DESC")
    @ransack_path = clients_path
  end

  def bday_today
    @ransack_clients = Client.bday_today.search(params[:clients_search], search_key: :clients_search)
    @clients = @ransack_clients.result.paginate(:page => params[:page], :per_page => 15).order("created_at DESC")
    @ransack_path = bday_today_clients_path
    render 'index'
  end

通知@ransack_path

现在,在索引视图中,ransack_path是自动设置的,因此,当您按Submit时,将在当前控制器中执行搜索!

= search_form_for @ransack_clients, as: :clients_search, url: @ransack_path do |f|

这是我的一部分路线:

  resources :clients do
    get :bday_today, on: :collection
    collection do
      match 'search' => 'clients#search', via: [:get, :post], as: :search
    end
  end