Rails:5.1.4
我试图搜索:immo_type或/和:地址
class SearchesController < ApplicationController
def home
@search = Purchase.find(params[:immo_type])
@purchases = Purchase.where("immo_type ILIKE ?", "%#{@search}%")
end
def index
@purchases = Purchase.all
@spurchases = Purchase.search_by_immo_type_and_address('@search')
end
端
从视图中我使用了simple_form_for。我不知道如何查看如何访问params [:immo_type]的内容。当我使用rails时,我发现了这条消息
无法通过&#39; id&#39; =
找到购买
我可以通过Purchase.all
查看所有购买内容<%= simple_form_for :immo_type, url: searches_url, method: :get do |f| %>
<%= f.hidden_field :immo_type, params[:immo_type] %>
<%= f.input :address, placeholder: 'Town', label: "Where" %>
<%= f.input :immo_type, placeholder: 'flat, house', label: "Flat or house" %>
<%= f.button :submit, "Rechercher", class: "btn btn-danger" %>
<% end %>
这是我的模特
class Purchase < ApplicationRecord
include PgSearch
pg_search_scope :search_by_immo_type_and_address, against: [:immo_type, :address]
belongs_to :user
has_many :users, :through => :searches
end
class Search < ApplicationRecord
belongs_to :user
belongs_to :leasing
belongs_to :purchase
end
我想从我的home.html.erb(根页)中搜索并在index.html.erb上显示结果
<ul>
<% @purchases.each do |purchase| %>
<li><%= link_to purchase.address %></li>
maison ou appartement : <%= purchase.immo_type %><br>
prix : entre <%= purchase.price_min %> et <%= purchase.price_max %><br>
<% end %>
</ul>
答案 0 :(得分:0)
所以,首先我需要使用带有主视图和索引视图的搜索控制器
class SearchesController < ApplicationController
def home
@searches = params[:immo_type]
end
def index
@purchases = Purchase.search_by_immo_type_and_address("#{params[:purchase][:immo_type]}")
end
end
这是我购买Pg_Search gem的模型
class Purchase < ApplicationRecord
belongs_to :user
has_many :users, :through => :searches
include PgSearch
pg_search_scope :search_by_immo_type_and_address, against: [:immo_type]
end
使用simple_form_for
的视图<%= simple_form_for :purchase, url: searches_url, method: :get do |f| %>
<%= f.input :address %>
<%= f.input :immo_type %>
<%= f.button :submit, "Rechercher"%>
<% end %>
我希望它可以帮到你!