Save search options from the advanced search form

I have an advanced search form, and I would like to give users the opportunity to save their options. How can i do this?

The user should be able to fill out their options for the advanced search form and click on the "Save this search" link in which their parameters will be stored. In addition, each user will have their own custom search parameters by default, so each time they are visited, they will not need to re-enter details.

I think it will be useful for many people in the future.

Search Controller:

 def new
    @search = Search.new
    render layout: 'new_application'
  end

  def create
    @search = Search.new(params[:search])
    if @search.save
        redirect_to @search
    else
      render 'new'
    end
  end

  def show
    @user = current_user
    @search = Search.find(params[:id])
    @users = @search.users
    render 'users/index', layout: 'new_application'    
  end

  def update
    @search = Search.find(params[:id])
    if @search.update_attributes(params[:search])
        redirect_to @search
    else
      render 'new'
    end
  end

  def index
    if location = Location.find_by_zipcode(params[:search])
        latitude  = location.latitude * Math::PI / 180 
        longitude = location.longitude * Math::PI / 180 

        locations = Location.search( 
          :geo   => [latitude, longitude], 
          :with  => {:geodist => 0.0..600_000.0}, 
          :order => 'geodist ASC',
          :per_page => 5_000
        ) 
        @users = User.where(zip_code: locations.map(&:zipcode))


          else
            @users = User.search(params[:search])
        end      
      end

  def min_age
    @min_age = params[:min_age].to_i.years
  end

  def max_age
    @max_age = params[:max_age].to_i.years
  end

  def youngest_age
  @youngest_age = params[:youngest_age].years
  end

  def oldest_age
  @oldest_age = params[:oldest_age].years
   end
end

Search Model:

 def users
    @users ||= find_users
  end

    private

    def find_users
      users = User.order(:id)
      users = users.where(gender: gender) if gender.present?
      users = users.where(zip_code: zip_code) if zip_code.present?
      users = users.where(children: children) if children.present?
      users = users.where(religion: religion) if religion.present?
      users = users.where(ethnicity: ethnicity) if ethnicity.present?

      if min_age.present? && max_age.present?
        min = [ min_age, max_age ].min
        max = [ min_age, max_age ].max
        min_date = Date.today - min.years
        max_date = Date.today - max.years
        users = users.where("birthday BETWEEN ? AND ?", max_date, min_date)
        users
      end
      users
    end
  end

Search Form:

  a.adv_search href="#" 
    = image_tag "adv_link.png"
    span Advanced Search
  = form_for @search do |f|
    .form_container
      .row
        .col
          label I am a
          select.large
            option Female
            option Male
        .select2
          .col.col2
            label Seeking
            = f.select :gender, %w(Female Male)
        .select1
          .col.col3
            label Ages
            = f.select :min_age, options_for_select((18..50),25), {}, {class: 'small'}
        .select5
          .col.col4
            label to
            = f.select :max_age, options_for_select((20..50),45), {}, {class: 'small'}
      .row
        .col.col5
          label Near
          = f.text_field :zip_code,placeholder: "enter zip here", class: 'text_input'
        .col.col6
          = f.select :children, ['I want kids now','I want one someday'], prompt: 'child preference'

        .select4
          .col.col6
            = f.select :religion, ['Agnostic', 'Atheist', 'Christian', 'Catholic', 'Buddhist', 'Hindu', 'Jewish', 'Muslim', 'Spiritual without affiliation', 'Other', 'None', 'Prefer not to say'], prompt: 'Religion'
        .select4
          .col.col7
            = f.select :ethnicity, ['Asian', 'Biracial', 'Indian', 'Hispanic/Latin', 'Middle Eastern', 'Native American', 'Pacific Islander', 'White', 'Other'], prompt: 'Ethnicity'
      .btm_sec
        ul.form_list
          li
            a href="#" 
              = image_tag "form_icon1.png"
              span.color Save this Search
          li
            a href="#" 
              = image_tag "form_icon2.png"
              span Load
          li
            a href="#" 
              = image_tag "form_icon3.png"
              span Reset
        input.find_btn type="submit" value="Find" /
    .btm_search_detail
+4
source share
2 answers

, . , , /. , rails-settings.

.. User - :

class User < ActiveRecord::Base
  has_settings do |s|
    s.key :search_options,  :defaults => { :gender => 'male'}
  end
end

/ :

user.settings(:search_options).gender = 'female'
user.save!

, , , - , .

+2

, :

#app/models/user.rb
Class User < ActiveRecord::Base
   has_one :search
   delegate :input, :advanced, :options, to: :search, prefix: true
end

#app/models/search.rb
Class Search < ActiveRecord::Base
   #fields id | user_id | input | other | advanced | options | created_at | updated_at
   belongs_to :user
end

, , .

, , , Search , , , Search :

#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
   before_action :set_search

   private

   def set_search
      @search_option = current_user.search_input if user_signed_in?
   end
end

, :

#app/views/shared/_search.html.erb
<%= form_tag search_path do |f| %>
    <%= text_field_tag :search, value: @search_option %>
    <%= submit_tag "Search" %>
<% end %>

,

-

Model

, , / . , ,

- , , , , .

Search, / :

#app/controllers/searches_controller.rb
Class SearchesController < ApplicationController
   def create
      current_user.search.create(input: params[:input], etc: "value") if params[:save_defaults].present?
   end
end

, , , :)

0

All Articles