как искать с помощью simple_form_for и pg_search gem

Рельсы: 5.1.4

Я пытаюсь выполнить поиск по :immo_type и/или :address

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]. Когда я использовал рельсы, у меня было это сообщение

Не удалось найти покупку с 'id'=

Я могу видеть все свои покупки с помощью 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

SearchesIndex

<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>

person Pierre Christophe Callac    schedule 10.12.2017    source источник


Ответы (1)


Итак, сначала мне нужно поработать с контроллером поиска с представлениями home и index.

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

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 %>

Я надеюсь, что это может помочь вам!

person Pierre Christophe Callac    schedule 12.12.2017