Home > Enterprise >  Running the search function in Rails will output all values
Running the search function in Rails will output all values

Time:09-22

What I want to come true

Implementing a search function in Rails. I've written the code, but the search function doesn't work. What should I do? I want to implement it without using gem this time.

Code

controller

class MoviesController < ApplicationController

・・・ 

  def search
    @movies = Movie.search(params[:search])
  end

・・・
end

model

class Movie < ApplicationRecord
    def self.search(search)
        if search
            Movie.where(['name LIKE ?', "%#{search}%"])
        else
            Movie.all
        end
    end
end

View index and search

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <%= stylesheet_link_tag 'movies', media: 'all'%>
    <title>Document</title>
</head>
<body>
<div class="search">
    <%= form_with url: search_path, method: 'get' do |form|%>
        <%= form.text_field :keyword, value: @keyword %>
        <%= form.submit "Search" %>
    <% end %>

    <div>
    </div>
</div>

    <div class="movies_item">
    <% @movies.each do |movie| %>
        <div class="movies_data">
            <%=image_tag movie.image_url %> 
            <p><%= movie.name %> </p>   
            <p>Details</p>
        </div>
    <% end %>
    </div>
</body>
</html>

Search results

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <%= stylesheet_link_tag 'movies', media: 'all'%>
    <title>Search</title>
</head>
<body>
<div class="search">
    <%= form_with url: search_path, method: 'get' do %>
        <%= text_field_tag :search%>
        <%= submit_tag "Search" %>
    <% end %>
</div>

    <% @movies.each do |movie| %>
            <div class="movies_data">
            <%= image_tag movie.image_url %> 
            <p><%= movie.name %> </p>   
            <p> Details </p>
        </div>
    <% end %>
</body>
</html>

routes


Rails.application.routes.draw do
  scope :admin do
    resources :movies
    get 'search', to: 'movies#search'
  end
end

Error

No error will be generated, but all the DB data will be output.

What I've tried.

・I removed Movie.all in model to check the behavior, but all values were output.

・Using rails c, where(name: "search_name") worked.

・However, if Movie.where(['name LIKE ? , "%search_name%"]) did not get the value. (The SQL search method is wrong. Or I don't know if the search function can't be executed because this can't be retrieved)

CodePudding user response:

You have to pass two separate parameters, not an array:

# correct
Movie.where('name LIKE ?', "%#{search}%")
# wrong
Movie.where(['name LIKE ?', "%#{search}%"])

CodePudding user response:

I changed :keyword, value: @keyword to :search in View index and search, and it displays correctly. :search

  • Related