Ruby on Rails: Advanced search

前端 未结 3 656
太阳男子
太阳男子 2020-12-03 09:24

I\'m having trouble understanding the best way to make a advanced search form. I have had a good search on the internet, looking at some ways, but I can\'t get them to work,

相关标签:
3条回答
  • 2020-12-03 09:27

    You can create a new controller called search.

    Your search form:

    <%= form_tag search_index_path, method: :get do %>
      <%= text_field_tag :project, params[:project] %>
      <%= text_field_tag :client, params[:client] %>
      <%= submit_tag "Search", name: nil %>
    <% end %>
    

    incude in your routes.rb:

    get "search/index"
    

    your search controller:

    def index
      #store all the projects that match the name searched
      @projects = Project.where("name LIKE ? ", "%#{params[:project]}%")  
      #store all the clients that match the name searched    
      @clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
    end
    

    Now you can play with @projects and @clients in the index view.

    Just be careful, because these variables might became nil if there is no match for the search.

    EDIT - I am assuming you have two models Project and Client - if you cannot create a new controller you can create the search action in your current controller.

    def search
      #store all the projects that match the name searched
      @projects = Project.where("name LIKE ? ", "%#{params[:project]}%")  
      #store all the clients that match the name searched    
      @clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
    end
    

    And than you can use the @projects and @clients in the search view.

    If you are trying to display the results in somewhere else (for example index view), you can just move the above to the correct action.

     def index
      ....
      #store all the projects that match the name searched
      @projects = Project.where("name LIKE ? ", "%#{params[:project]}%")  
      #store all the clients that match the name searched    
      @clients = Client.where("name LIKE ? ", "%#{params[:client]}%")
    end
    

    EDIT 2 - OK, you are trying to search by a combination of fields in the same model:

    You and change your search method to add these two fields:

    def self.search(search_project, search_client) 
      return scoped unless search_project.present? || search_client.present?
      where(['project_name LIKE ? AND client LIKE ?', "%#{search_project}%", "%#{search_client}%"])
    end
    

    But please note the || will return scope if your search_project OR search_client are not present, you can change for AND (&&) if you prefer.

    Also, the AND will return only if both match, I mean the combination of search... You can also change it to OR if you want.

    Having the search form:

    Your search form:

    <%= form_tag search_index_path, method: :get do %>
      <%= text_field_tag :project, params[:project] %>
      <%= text_field_tag :client, params[:client] %>
      <%= submit_tag "Search", name: nil %>
    <% end %>
    

    Then your controller must send the combination to the model:

    @project_search = Project.search(params[:project], params[:client]).all
    

    I think it will solve the problem...

    0 讨论(0)
  • 2020-12-03 09:32

    I've been using MetaSearch in my application and found it quite convenient. If you've already considered it, what problems did you have?

    There's also Ransack by the same author, it's a successor to MetaSearch.

    0 讨论(0)
  • 2020-12-03 09:36

    A simple explanation can be found in this rails cast

    Basically, we have to test if the params contain a specific field and create the filter. See the example below:

    def find_products
      products = Product.order(:name)
      products = products.where("name like ?", "%#{keywords}%") if keywords.present?
      products = products.where(category_id: category_id) if category_id.present?
      products = products.where("price >= ?", min_price) if min_price.present?
      products = products.where("price <= ?", max_price) if max_price.present?
      products
    end 
    

    An alternative is Ransack. Ransack enables the creation of both simple and advanced search forms for your Ruby on Rails application

    0 讨论(0)
提交回复
热议问题