Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用一个文本字段和一个类别选择类型字段进行pg_搜索_Ruby On Rails_Ruby On Rails 3_Postgresql_Pg Search - Fatal编程技术网

Ruby on rails 使用一个文本字段和一个类别选择类型字段进行pg_搜索

Ruby on rails 使用一个文本字段和一个类别选择类型字段进行pg_搜索,ruby-on-rails,ruby-on-rails-3,postgresql,pg-search,Ruby On Rails,Ruby On Rails 3,Postgresql,Pg Search,RubyonRails新手。我正在尝试使用pg_搜索gem,但遇到了一些困难是的,我正在查看文档。我想要一个既有文本字段又有选择字段类别的搜索表单。以下是我目前掌握的代码: 联系_info.rb class ContactInfo < ActiveRecord::Base include PgSearch ... pg_search_scope :search_by_category, :against => :previous_value scope :in_cat

RubyonRails新手。我正在尝试使用pg_搜索gem,但遇到了一些困难是的,我正在查看文档。我想要一个既有文本字段又有选择字段类别的搜索表单。以下是我目前掌握的代码:

联系_info.rb

class ContactInfo < ActiveRecord::Base
  include PgSearch

 ...
  pg_search_scope :search_by_category, :against => :previous_value
  scope :in_category, lambda { |category_id|
    where(:category_id => category_id)
  }
end
我知道问题出在这方面:

@contact_infos = ContactInfo.search_by_category(params[:query])
但我不确定如何从视图home\index.html.erb中准确传递这两个变量


谢谢

好吧-我想出来了。如果它能帮助某人

在模型中,请联系_info.rb

class ContactInfo < ActiveRecord::Base
  include PgSearch

 ...
  pg_search_scope :search_by_category, :against => :previous_value
  scope :in_category, lambda { |category_id|
    where(:category_id => category_id)
  }
end
在控制器home_controller.rb中

class HomeController < ApplicationController
  def index

  if params[:search_by_category].nil?
    @contact_infos = ContactInfo.search(params[:query])
  else
    tmp = params[:search_by_category]
    @contact_infos = ContactInfo.search_by_category(params[:query]).in_category(tmp[:category_id])
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @contact_infos }
    end
  end
 end
end
在视图home\index.html.erb中

...
    <%= form_for :search_by_category, :url => {:controller => 'home', :action => 'index'}, :html => {:method => 'get'} do |f| %>
    <%= text_field_tag :query %>
    <%= f.label :category_id, "Category" %>
    <%= f.collection_select :category_id, Category.all,
                            :id, :name,
                            {:prompt => 'All'} %>
    <br/>
    <%= f.submit "Search" %>
<% end %>
...

好吧,我知道了。如果它能帮助某人

在模型中,请联系_info.rb

class ContactInfo < ActiveRecord::Base
  include PgSearch

 ...
  pg_search_scope :search_by_category, :against => :previous_value
  scope :in_category, lambda { |category_id|
    where(:category_id => category_id)
  }
end
在控制器home_controller.rb中

class HomeController < ApplicationController
  def index

  if params[:search_by_category].nil?
    @contact_infos = ContactInfo.search(params[:query])
  else
    tmp = params[:search_by_category]
    @contact_infos = ContactInfo.search_by_category(params[:query]).in_category(tmp[:category_id])
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @contact_infos }
    end
  end
 end
end
在视图home\index.html.erb中

...
    <%= form_for :search_by_category, :url => {:controller => 'home', :action => 'index'}, :html => {:method => 'get'} do |f| %>
    <%= text_field_tag :query %>
    <%= f.label :category_id, "Category" %>
    <%= f.collection_select :category_id, Category.all,
                            :id, :name,
                            {:prompt => 'All'} %>
    <br/>
    <%= f.submit "Search" %>
<% end %>
...