【问题标题】:How can I save parameters in my form?如何在表单中保存参数?
【发布时间】:2013-12-13 23:43:53
【问题描述】:

对于我的搜索表单,我在这里使用解决方案: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

但是,现在表单在= f.input :first_name 行处抛出了NoMethodErrorundefined method 'first_name' for #<ActionController::Parameters:0x007fa5688aa268> 如何将我的search_query 传递回表单?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-10
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2023-03-31
      • 1970-01-01
      • 2022-11-25
      相关资源
      最近更新 更多