【问题标题】:Show filterrific form in different controller view在不同的控制器视图中显示过滤形式
【发布时间】:2015-07-14 14:06:34
【问题描述】:

我想在我的应用程序的登录页面中显示过滤表单。如何让表单响应不同的控制器操作?

以下代码来自 Filterrific 示例应用程序。我想搜索和过滤学生模型。我希望我的应用程序的用户能够从 Welcome Controller 的主页搜索学生。

<%# app/views/students/index.html.erb %>
<h1>Students</h1>

    <%= form_for_filterrific @filterrific do |f| %>
  <div>
    Search
    <%# give the search field the 'filterrific-periodically-observed' class for live updates %>
    <%= f.text_field(
      :search_query,
      class: 'filterrific-periodically-observed'
    ) %>
  </div>
  <div>
    Country
    <%= f.select(
      :with_country_id,
      @filterrific.select_options[:with_country_id],
      { include_blank: '- Any -' }
    ) %>
  </div>
  <div>
    Registered after
    <%= f.text_field(:with_created_at_gte, class: 'js-datepicker') %>
  </div>
  <div>
    Sorted by
    <%= f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %>
  </div>
  <div>
    <%= link_to(
      'Reset filters',
      reset_filterrific_url,
    ) %>
  </div>
  <%# add an automated spinner to your form when the list is refreshed %>
  <%= render_filterrific_spinner %>
<% end %>

<%= render(
  partial: 'students/list',
  locals: { students: @students }
) %>

此代码有效,因为我从学生控制器的 index.html 页面进行了过滤设置。我想在我的欢迎控制器的主页中显示此表单,以便当用户输入他们的过滤条件并进行搜索时,它会将他们带到学生索引页面以及他们的搜索结果。我如何让它从那里工作?我应该对表格进行哪些更改?

查看此页面以获取示例应用程序。 Example Filterrific application

感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails-4 filterrific


    【解决方案1】:

    您需要为所需的行为执行以下操作:

    • 覆盖form_for_filterrific 上的:url 选项,使其提交到StudentsController#index 而不是主页。
    • 关闭 ajax 自动提交并手动提交表单。 Ajax 行为是基于表单的id attr 添加的。所以关闭 Auto ajax submit 最简单的方法就是更改 id。
    • 在表单中添加Submit 按钮以供手动提交。

    以下是相关代码:

    <%= form_for_filterrific(
          @filterrific, 
          url: students_path, 
          html: { id: :manual_filterrific_form }
        ) do |f| %>
      [form inputs go here ...]
      <%= f.submit "Show students" %>
    <% end %>
    

    现在表单显示在主页上。当用户设置过滤器并点击“显示学生”时,他们将被带到StudentsController#index 并查看过滤后的学生列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多