Rails - 搜索结果未过滤。返回所有空房

时间:2017-09-26 23:19:02

标签: ruby-on-rails

任务范围:用户从下拉菜单(单人/双人卧室)中选择一个选项,点击搜索按钮并获取所有相关结果。然后在搜索页面上,用户可以使用可用的过滤器(电视/淋浴)进一步细化结果。由于某些原因,这些操作都没有修改搜索结果 - 页面显示数据库中的所有可用列表,而不是符合条件的列表。

我做错了什么?

这是我到目前为止所做的:

HOME SEARCH BAR

<%= form_tag search_path, method: :get do %>
    <div class="row">
      <div class="col-md-7">
        <%= select_tag :bedroom, options_for_select([['Single', 1], ['Double', 2]]), class: "form-control" %>
      </div>
      <div class="col-md-2">
        <%= submit_tag "Search", class: "btn btn-normal btn-block" %>
      </div>
    </div>
<% end %>

搜索页面

<div class="col-sm-3">
    <%= search_form_for @search, url: search_path, remote: true do |f| %>
        <div class="row">
          <div>
            <%= check_box_tag "q[is_tv_eq]", true %> TV
          </div>
          <div>
            <%= check_box_tag "q[is_shower_eq]", true %> Shower
          </div>
        </div>
        <div class="row text-center">
          <%= f.submit "Search", class: "btn btn-form" %>
        </div>
    <% end %>
    <%= render partial: "rooms/rooms_list", locals: {rooms: @arrRooms} %>
</div>

ROOM LIST PARTIAL

<% rooms.each do |room| %>
    <div class="row">
          <%= image_tag room.cover_photo(:medium) %>
          <%= link_to room.user_id, room %>
          <%= room.price %> - <%= room.bedroom %>
          <div id="star_<%= room.id %>"></div> <%= pluralize(room.average_rating, "review") %>
    </div>
    <script>
      $('#star_<%= room.id %>').raty({
        path: '/assets',
        readOnly: true,
        score: <%= room.average_rating %>
      });
    </script>
<% end %>

ROOM MODULE

class CreateRooms < ActiveRecord::Migration[5.0]
  def change
    create_table :rooms do |t|
      t.string :bedroom
      t.integer :price
      t.boolean :active
      t.timestamps
    end
  end
end

PAGE CONTROLLER

def search
    # STEP 1
    if params[:search].present? && params[:search].strip != ""
      session[:loc_search] = params[:search]
    end

    # STEP 2
    if session[:loc_search] && session[:loc_search] != ""
      @rooms_bedroom = Room.where(active: true, bedroom: session[:loc_search]).order(:price)
    else
      @rooms_bedroom = Room.where(active: true).all
    end

    # STEP 3
    @search = @rooms_bedroom.ransack(params[:q])
    @rooms = @search.result
    @arRoooms = @rooms.to_a
end

在房间概述页面中定义卧室选项(不确定是否有帮助)

<%= form_for @room do |f| %>
    <div class="form-group">
        <label> Bedroom Type </label>
        <%= f.select :bedroom, [["Single", "Single"], ["Double", "Double"]],
        id: "bedroom", prompt: "Select...", class: "form-control" %>
        </div>
    </div>
    <div><%= f.submit "Save", class: "btn btn-normal" %></div>
<% end %>

由于

0 个答案:

没有答案