简体   繁体   English

PostgreSQL全文搜索不会从Rails中的表单中获取参数

[英]PostgreSQL Full Text search does not take params from form in Rails

I'm trying to replicate railscasts #343 in my rails app with this code: 我正在尝试使用以下代码在我的rails应用程序中复制railscast#343:

class Article < ActiveRecord::Base
  def self.text_search(query)
    if query.present?
      where("description @@ :q", q: query)    
    else
      where(nil)
    end
  end

class ArticlesController < ApplicationController
  def index
    @articles = Article.text_search(params[:query])
  end

and this view form: 而这种观点形式:

<%= simple_form_for articles_path, method: :get do |f| %>
  <%= f.input :query, autofocus: true, label: false %>
  <%= f.submit 'Search', name: nil %>
<% end %>

In console text_search method works. 在控制台text_search方法工作。 From log: Parameters: {"utf8"=>"✓", "/en/articles"=>{"query"=>"word"}, "locale"=>"en"} I see that params are sent but I still get all articles listed and no search is performed: Article Load (0.5ms) SELECT "articles".* FROM "articles" 来自日志: Parameters: {"utf8"=>"✓", "/en/articles"=>{"query"=>"word"}, "locale"=>"en"}我看到params被发送但是我仍然列出所有文章并且没有执行搜索: Article Load (0.5ms) SELECT "articles".* FROM "articles"

I have rails 4.2.2, PostgreSQL 9.3.7, ruby 2.2.5 我有rails 4.2.2,PostgreSQL 9.3.7,ruby 2.2.5

You need to pass the search params as: 您需要将搜索参数传递为:

Article.text_search(params[:search]["query"]) 

And add @article to the simpleform_for line: 并将@article添加到simpleform_for行:

<%= simple_form_for @article, articles_path, method: :get do |f| %>

And add @article = Article.new to your new action in the controller. 并将@article = Article.new添加到控制器中的新操作。

Your attribute will then be nested inside :article! 然后你的属性将嵌套在里面:文章! You can see it in the console output that the parameters sent by the articles form will always be wrapped in the class name the form is for (Article) via Rails Form helpers. 您可以在控制台输出中看到,文章表单发送的参数将始终包含在表单所用的类名称(文章)中,通过Rails Form帮助程序。

Update: 更新:

Because the simple_form_for wasn't passed @article specifically, it will be wrapped in the name of the submit action which in this case is search. 因为simple_form_for没有特别传递给@article ,所以它将被包装在提交操作的名称中,在这种情况下是提交操作。 Leaving you with: params[:search]["query"] 离开你: params[:search]["query"]

A more Rails approach would be to include @article thus wrapping your parameter hash as described above ( params[:article][:query] ): 更多的Rails方法是包含@article从而包装你的参数哈希,如上所述( params[:article][:query] ):

<%= simple_form_for @article, {url here}, {options like 'method: :get' or 'remote: true' here } do |f| %>

For extra info. 有关额外信息。 since everyone loves extra info, Rails provides a way to specify the wrapper name for your params hash in the form_for call thus you can also do something like: 由于每个人都喜欢额外的信息,Rails提供了一种在form_for调用中为params哈希指定包装器名称的方法,因此您还可以执行以下操作:

<%= simple_form_for (@article, as: :search), articles_path, method: :get do |f| %>

This would be best practice if you really want to keep the params wrapped in 'search' IMO because you're specifying the class object in the form call and letting yourself know what it's doing with that object. 如果你真的想让params包含在'search'IMO中,这是最好的做法,因为你在表单调用中指定了类对象,并让自己知道它对该对象做了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM