Home>

I am creating a site where you can post blogs with ruby ​​on rails.
I would like to implement a function to search the content of the article posted there.
As a prerequisite for creating a search form

  • You are using form_with
  • Actions are defined by the name "seach" other than the basic actions of rails.
  • The search condition is a post that includes the text of the article
  • We have implemented a simple tagging function before implementing the search function.
    is.
  • I want to display the search form on all pages, so I write it in application.html.rb
  • I'm using bootstrap 4

I'm still new to programming, so I tried to find out for myself, but I couldn't find a solution.
I'm sure there are many things that you can't reach with your first question, but please help us.

The screen does not change to the search result page even if I enter the characters I want to search and press the search button.
No error message is displayed.

Corresponding source code

Routing

config/routes.rb
Rails.application.routes.draw do
  devise_for: users
  root to:'posts # index'
  resources: posts do
    resources: comments, only :: create
    collection do
      get'search'
    end
  end
  resources: users, only :: show
end


Controller

app/controllers/posts_controller.rb
def search
    @posts = Post.search (params [: keyword])
--The following is described when the search function is implemented -
    return nil if params [: input] == ​​""
    tag = Tag.where (['name LIKE?', "% # {params [: input]}%"])
    render json: {keyword: tag}
  end

model

app/models/post.rb
class Post</pre>
<p>View screen (search form)</p>
<pre><code><form>
<% = form_with (url: search_posts_path, local: true, method:: get) do | form |%>
<% = Form.text_field: keyword, placeholder: "Search for posts", class: "form-control mr-sm-2"%>
 <% = Form.submit "Search", class: "nav-alink btn btn-outline-secondary mr-2"%>
 <% end%>
</form>

View screen (search results)


<% @ post.each do | post |%>
  
      
      <% = image_tag post.image, size: "100x200", class: "card-img-top"%>
           <% = post.title%>
          <p><% = post.text%></p>
          <ul>
          <li>
          <% post.tags.each do | tag |%>
            #<% = tag.name%>
          <% end%></li>
          </ul>
          <% = link_to "Go to detail page", post_path (post.id), method:: get, class: "btn btn-outline-secondary btn-block"%>
        
      
<% end%>
What I tried

Here's an easy-to-understand summary of how to implement search functionality in your Rails app.I tried using form_tag instead of form_with, but I still couldn't transition the screen.

When you move to the search result screen directly from the URL, all the content of the posted article is displayed as a character string.

It may be a misguided question due to lack of knowledge, but I am in trouble because I cannot move forward.

I would really appreciate it if you could lend me your wisdom.

Supplementary information (FW/tool version, etc.)

ruby_version 2.6.5

Rails 6.0.3.4

  • Answer # 1

    I solved it myself.
    The cause was that the search action of the post controller had two definitions, an incremental search function for tagging and a search function.

    Before correction

    app/controllers/posts_controller.rb
    def search
        @posts = Post.search (params [: keyword])
    --The following is described when the search function is implemented -
        return nil if params [: input] == ​​""
        tag = Tag.where (['name LIKE?', "% # {params [: input]}%"])
        render json: {keyword: tag}
      end


    Revised

    def search
        @posts = Post.search (params [: keyword])
    end
    --Incremental search action renamed to tag -
    def tag
        return nil if params [: input] == ​​""
        tag = Tag.where (['name LIKE?', "% # {params [: input]}%"])
        render json: {keyword: tag}
    end