搜索未找到正确的结果

时间:2016-12-01 18:27:12

标签: ruby-on-rails forms search

我正在学习如何创建rails搜索栏。我有一个名为电影的模特。我想按标题搜索电影并显示结果。我在" new"查看,不是"索引。"我也在我的结果上使用will_paginate。目前,即使电影存在,我的搜索也没有结果。如果我不进行搜索,我仍会看到我的所有电影......但显然,我还需要搜索功能。有人可以帮忙吗?

新电影观点:

<div class="row">
<div class="col-xs-12">
    <h2 class="white">Home</h2>
    <hr />
    <h4 class="white">Add Movie</h4>
    <%= form_for @movie, class: 'form-horizontal' do |m| %>
        <div class="form-group">
            <label for="title" class="white">Title: </label><br/>
            <%= m.text_field :title,  class: 'form-control' %>
        </div>
        <div class="form-group"> 
            <label for="title" class="white">Year: </label><br/>
            <%= m.text_field :year, class: 'form-control' %>
        </div>
        <div class="form-group">
            <%= m.submit %> 
        <div class="form-group">
    <% end %>
</div>  
</div>

<hr />

<div class="row">
<div class="col-xs-12">
<h4 class="white">Database</h4>
<%= form_tag new_movie_path, :method=> 'get' do %>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search" %>
<% end %>
<br />
    <% if @movies.exists? %>
    <div class="bg_white">
        <table class="table table-hover table-striped">
        <tr>
            <th>
                Title
            </th>
            <th>
                Status
            </th>
            <th>
                Year
            </th>
            <th>
                Delete
            </th>
            <th>
                Edit
            </th>
        </tr>
        <% @movies.each do |m| %>
        <tr>
            <td>
                <%= link_to m.title, new_movie_rental_path(m) %>
            </td> 
            <td> 
                <%= m.status %>
            </td>
            <td>
                <%= m.year %>
            </td>   
            <td> 
                <%= link_to "Delete", movie_path(m), method: :delete %>
            </td>  
            <td>
                <%= link_to "Edit", edit_movie_path(m) %>
            </td>  
        </tr>
        <% end %>
        </table>
    </div>
    <%= will_paginate @movies, class: 'white' %>
    <% else %>
        <p class="white">No movies have been entered</p>
    <% end %> 
</div>  
</div>
<div class="row">
<div class="col-xs-12">
    <hr />
    <%= link_to "Add Customer", new_customer_path, class: 'white' %>
</div>
</div>
<br /><br />

电影模特:

class Movie < ApplicationRecord
has_many :rentals, dependent: :destroy 


def status 
    if self.rentals.empty?
        return "In Stock"
    else
        self.rentals.order(borrowed_on: :desc).each do |x|
            if !x.returned_on.nil?
                return "In Stock"
            else 
                return "Rented"
            end
        end
    end
end

def self.search(search)
    if search
        @movies = Movie.where(["title","%#{[:search]}%"])
    else
        all
    end 
end 

end

电影控制器

 def new
    @movie = Movie.new
    @movies = Movie.search(params[:search]).order(title: :asc).paginate(:page => params[:page], :per_page => 15) 
 end

1 个答案:

答案 0 :(得分:1)

我说问题就在于你作为搜索范围的这个类方法:

def self.search(search)
  if search
    @movies = Movie.where(["title","%#{[:search]}%"])
  else
    all
  end 
end

我猜你试图将搜索参数插入到查询中,但是在这种情况下使用#{}你传递符号搜索而不是插值。因此,您的查询最终为SELECT 1 AS one FROM "movies" WHERE (title)。但更重要的是,通过直接插入查询,您将非常容易受到SQL注入的攻击。如果用户在')DROP TABLE MOVIES ('之类的搜索字段中输入内容,该怎么办?我会像这样重写它:

def self.search(search = nil)
  if search
    Movie.where('title LIKE :search', search: "%#{search}%")
  else
    Movie.scoped
  end
end

另请注意,当搜索为空时,我将如何返回Movie.scoped而不是全部;返回一个关系,.all返回一个电影对象数组,如果你要调用它上面的order和其他链,你就不想要了。

读取sql注入和参数插值: http://rails-sqli.org/#where   http://api.rubyonrails.org/v5.0.0.1/classes/ActiveRecord/QueryMethods.html#method-i-where