如何在表单中保存参数?

时间:2013-12-13 15:12:38

标签: ruby-on-rails

对于我的搜索表单,我在这里使用解决方案:https://stackoverflow.com/a/12622896/1943735

我的搜索页面如下:

= simple_form_for search_query, :url => :search, do |f|
    = f.input :first_name
    = f.input :last_name
    ...

Table To Display Results is here

在我的控制器中,我有这个:

def search
    search_params = params[:search_query]
    @people = Result of some searching
end

上述代码有效,但用户无法看到他们只是在表单中键入了哪些值而只获得了结果。表单再次呈现,现在为空白。

我对解决方案的尝试是:

def search
    search_params = params[:search_query]
    @people = Result of some searching
    @search_query = params[:search_query]
end

但是,现在表单会在行NoMethodError处引发undefined method 'first_name' for #<ActionController::Parameters:0x007fa5688aa268> = f.input :first_name {}}如何将search_query传回表单?

1 个答案:

答案 0 :(得分:1)

我认为这里的诀窍是使用GET而不是POST,这样搜索参数将在您的视图中保持可见。

以下是我写的一个正在运行的应用程序的一个例子......

 def index
    authorize! :index, Employee

    params[:filter] ||= 'All' # Default to All Employees
    params[:search] ||= ''    # Default to blank
    page_title ||= params[:filter].titleize

    # Clear the search field if a filter option was selected
    if session[:employee_filter] != params[:filter]
      session[:employee_filter] = params[:filter]
      params[:search] = ''
    end

    # Generate an array of our filter elements (include all departments)
    @page_links = %w[All Featured Admins]
    Department.all.each do |department|
      @page_links << [department.title, department.name]
    end

    # Check and execute the filter option (defaults to All above)
    case params[:filter]
      when 'All'
        @employees  = Employee.search(params[:search]).by_name.page(params[:page]).per(15)
      when 'Featured'
        # We need this to be all on one page so that we can drag-and-drop position employees
        @employees  = Employee.search(params[:search]).is_featured.page(params[:page]).per(99999)
      when 'Admins'
        @employees  = Employee.search(params[:search]).admins.by_name.page(params[:page]).per(15)
        @page_title = 'Site Administrators'
      else
        @employees  = Employee.search(params[:search]).dept(params[:filter]).by_index.page(params[:page]).per(15)
    end

    @page_title ||= "#{page_title} Employees"
    @page_title = "'#{params[:search]}' in #{page_title}" unless params[:search].blank?

  end

这是Employee模型中的搜索方法:

 def self.search(search)
    if search
      results = where('last_name ILIKE ? OR first_name ILIKE ? OR email ILIKE ?', "%#{search}%", "%#{search}%", "%#{search}%")
      results = where('title ILIKE ?', "%#{search}%") if results.blank?
      results = joins(:location).where('locations.name ILIKE ?', "%#{search}%") if results.blank?
      return results
    end
  end

最后,索引的搜索表单(采用HAML格式):

.sort-index
  = form_tag admin_employees_path, method: 'get' do
    = select_tag :filter, options_for_select(@page_links, params[:filter]), onchange: "this.form.submit();"
    %br
    = text_field_tag :search, params[:search], placeholder: 'Search Name, Title, or Location', size: 35
    = submit_tag :search, name: nil, hidden: true

那里有一些额外的搜索内容(比如额外的过滤器),所以让我知道这对你来说太复杂了。