Home>

I have a search function implemented in rails. So I want to implement a search function with multiple words by white space, but it doesn't work.

The word search feature is now part of form_with, combined with other advanced searches.
In addition, since they use scope in the model, I do not know where to write multi-word processing.
When implemented before, it was only a word search, so we wrote a multiple word function in the controller, but this time using the scope in the model as described above, so I don't know how to assemble it. It is a state.
When using scope, how do I write code that is normally written in a controller such as a multi-word function?
Thanks for your professor.

<% = form_with (scope:: search, url: search_posts_path, method:: get, local: true) do | f |%>
             Advanced search 
              <i></i><% = f.label: Add keyword%>
                <% = f.text_field: name, value: @search_params [: name],

 class: "search-text"%>
              <i></i><% = f.label: price%>
                <% = f.number_field: price_from, placeholder: "\ Min", value: @search_params [: price_from],

 class: "search-number"%>~<% = f.number_field: price_to, placeholder: "\ Max", value: @search_params [: price_to],

 class: "search-number"%>
              <i></i><% = f.label: Category%>
                <% = f.select: category, [["Men", "Men" "],

 ["Ladies", "Ladies"],

["Hobby", "Hobby"],

["Game", "Game"],

["Home appliance", "home appliance"],

["Interior", "Interior"],

["Goods", "Goods"],

["Sports", "Sports"],

["Other", "Other"]],{selected: @search_params [: category]},

 {class: "search-category"}%>
                <i></i><% = f.label: Product status%>
                  
                    <% = f.check_box: status, {multiple: true, include_hidden: true},

 'New', nil%>
                    <% = f.label: New product%>
                  
                    <% = f.check_box: status, {multiple: true, include_hidden: true},

 'No noticeable scratches or dirt', nil%>
                    <% = f.label: No noticeable scratches or dirt%>
                  
                  
                    <% = f.check_box: status, {multiple: true, include_hidden: true},

 'Scratched and dirty', nil%>
                    <% = f.label: Scratches and dirt%>
                  
                    <% = f.check_box: status, {multiple: true, include_hidden: true},

 'Overall bad', nil%>
                    <% = f.label: Overall bad state%>
                  
                    
                      <% = link_to "Clear", search_posts_path%>
                    <% = f.submit 'Done', class: "search-button"%>
                    
          <% end%>
def search
    @search_params = post_search_params
    @posts = Post.search (@search_params) .order (sort)
  end
  private
  def post_search_paramsparams.fetch (: search, {}). permit (: name,: price_from,: price_to,: category, status: [])
  end
scope: search,->(search_params) do
    return if search_params.blank?
    name_like (search_params [: name])
    .price_from (search_params [: price_from])
    .price_to (search_params [: price_to])
    .category_like (search_params [: category])
    .status_like (search_params [: status])
  end
  scope: name_like,->(name) {where ('name LIKE?', "% # {name}%") if name.present?}
  scope: price_from,->(from) {where ('?<= price', from) if from.present?}
  scope: price_to,->(to) {where ('price<=?', to) if to.present?}
  scope: category_like,->(category) {where (category: category) if category.present?}
  scope: status_like,->(status) {where (status: status) if status.present?}

Add from here

def search
    sort = params [: sort] || "created_at DESC"
    @posts_else = Post.all.order (sort)
    @search_params = post_search_params
    # @ posts = Post.search (@search_params) .order (sort)
  end

        <% @ posts_else.each do | post |%>
          <% = link_to post_path (post.id) do%>
            
               \<% = converting_to_jpy (post.price)%>
                <% = image_tag post.image.url%>
                  <% = truncate (post.name, length: 20)%>
            
          <% end%>
        <% end%>
      
    
  <% end%>
  • Answer # 1

    I don't think so hard. scope means that the (last) expression written there is added to the search expression.
    scope: name_like,->(name) {where ('name LIKE?', "% # {name}%") if name.present?}
    Please put in the {} of "I wrote a multi-word function in the controller".
    Make the last expression a where () or where chain.
    It is easier to write if if..end is less difficult.

Related articles