Rails 未在搜索表单上传递 DB 查询 [英] Rails not passing the DB Query on search form

查看:25
本文介绍了Rails 未在搜索表单上传递 DB 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里看起来更好,这是日志,它传递了参数,但它没有传递带有该参数的查询

 开始 GET "/items?utf8=%E2%9C%93&search=blanca" for 65.34.251.106 at 2016-08-30 03:55:51 +0000由 ItemsController#index 处理为 HTML参数:{"utf8"=>"✓", "search"=>"blanca"}用户负载 (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ?ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]]项目负载 (0.3ms) SELECT "items".* FROM "items" LIMIT 50 OFFSET 0项目加载 (0.2ms) SELECT "items".* FROM "items" ORDER BY "items"."id" ASC LIMIT 1 OFFSET 0渲染的 items/_items.html.erb (3.2ms)在布局/应用程序中渲染 items/index.html.erb (4.9ms)

这是控制器:

def 索引@items = Item.search(params[:search])结尾

这是模型:

 类项目 

和视图中的搜索表单:

<%= form_tag items_path, :method =>'get', :id =>items_search"做%><p><%= text_field_tag :search, params[:search], class: "form-control" %><%= submit_tag "搜索", :name =>无,类:btn btn-danger"%></p><div id="items"><%= render 'items' %>

<%结束%>

我有一个 _items.html.erb 文件,其中包含要呈现的项目,该部分有效,因为无论我在搜索栏上输入如何,它始终显示所有项目

这是我尝试在控制台中使用 .search 方法时的输出

 2.3.0 :041 >项目.搜索(布兰卡")NoMethodError: # 的未定义方法`search'来自/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/dynamic_matchers.rb:26:in `method_missing'来自 (irb):41来自/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'来自/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'来自/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'来自/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'来自/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in ‘’来自 script/rails:6:in `require'来自 script/rails:6:in `<main>'

解决方案

您确定在 item.rb 中定义了类方法 search 吗?因为错误说没有在 Item 类上定义 search 方法.但是,请尝试以下方法是否适合您.

您只想搜索表单是否正在提交.您可以使用 present? 方法来了解搜索词是否实际通过.

将您的控制器修改为

def 索引@items = params[:search].present??Item.search(params[:search]) : Item.all结尾

请注意,只有当对象不是空白时,对象才present.这很方便,因为如果有人在表单中输入",您不应该在 nombre 字段中搜索空白区域,而是显示 items 表中的所有记录.

如果表单没有提交,Item.all 将被执行.

修改你的类方法

def self.search(search_term)where("nombre LIKE ?", "%#{search_term}%")结尾

您不需要 Item.where,因为 Item 类是调用 where 方法的当前上下文.请注意,我已将模式更改为 %#{search_term}% 因为您并不总是希望匹配以某种模式结尾的字符串.

希望这会有所帮助!

Here what looks better, this is the log, its passing the param, but its not passing a query with that param

Started GET "/items?utf8=%E2%9C%93&search=blanca" for 65.34.251.106 at 2016-08-30 03:55:51 +0000
Processing by ItemsController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"blanca"}
User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
Item Load (0.3ms)  SELECT  "items".* FROM "items" LIMIT 50 OFFSET 0
Item Load (0.2ms)  SELECT  "items".* FROM "items"  ORDER BY "items"."id" ASC LIMIT 1 OFFSET 0
Rendered items/_items.html.erb (3.2ms)
Rendered items/index.html.erb within layouts/application (4.9ms)

This is the controller:

def index
 @items = Item.search(params[:search])
end

and this is the model:

 class Item < ActiveRecord::Base
  has_many :stocks
  attr_accessible :nombre, :espesor, :material, :quantity
  accepts_nested_attributes_for :stocks
   attr_accessible :stocks_attributes
   self.per_page = 50
   #  def self.search(search)
   #     Item.where("nombre LIKE ?", "%#{search}%")

   #  end

    def self.search(search_term)
    where("nombre LIKE ?", "%#{search_term}%")
    end

    protected
    end

and the search form in the view:

<%= form_tag items_path, :method => 'get', :id => "items_search" do %>
 <p>
  <%= text_field_tag :search, params[:search], class: "form-control" %>
  <%= submit_tag "Search", :name => nil, class: "btn btn-danger" %>
 </p>

<div id="items"><%= render 'items' %>
</div>
<% end %> 

I have a _items.html.erb file which has the items to render, that part works because no matter my input on the search bar, it always shows all the items

This is the ouput when i try to use .search method in the console

 2.3.0 :041 > Item.search("blanca")
 NoMethodError: undefined method `search' for #<Class:0x0000000203fc58>
    from /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-     4.2.6/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
      from (irb):41
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

解决方案

Are you sure you defined the class method search in item.rb? Cause the error says that there's no search method defined on the Item class. However, try if the following works for you.

You only want to search if the form is being submitted. You can use the present? method to know if the search term was actually passed.

Modify your controller to

def index
  @items = params[:search].present? ? Item.search(params[:search]) : Item.all
end

Note that an object is present only if its not blank. This is handy because if someone enters " " in the form, you shouldn't search for an empty space in the nombre field but display all the records in the items table.

If the form was not submitted, Item.all will be executed.

Modify your class method to

def self.search(search_term)
  where("nombre LIKE ?", "%#{search_term}%")
end

You don't need a Item.where because the class Item is the current context on which the where method is called. Note that I have changed the pattern to %#{search_term}% cause you don't always want to match strings ending with some pattern.

Hope this helps!

这篇关于Rails 未在搜索表单上传递 DB 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆