简体   繁体   English

从下拉列表中筛选索引页面上的结果

[英]Filter results on index page from dropdown

I'm using rails 3 and I have two models, venues and areas where each area has many venues and each venue belongs to one area. 我正在使用轨道3,我有两个模型,场地和区域,每个区域有许多场地,每个场地属于一个区域。

I'm trying to find a way of filtering the venue records displayed in the venue index by what area is selected in a dropdown box on the same page. 我正在尝试找到一种方法来过滤场地索引中显示的场地记录,在同一页面的下拉框中选择了哪个区域。

The dropdown box currently displays all my area records as I would like but after selecting an area and clicking the submit button I would like the index page to reload and only display the partials of the venue records with the same area as the one selected in the dropdown box. 下拉框当前显示我想要的所有区域记录,但是在选择区域并单击提交按钮后,我希望索引页面重新加载,并且仅显示场地记录的部分区域,其区域与在下拉框。

The scopes I have in the model display the correct venues when called in the rails console or by changing the def index in the controller to @venues = Venue.north / venue.south / venue.west. 我在模型中的示波器在rails控制台中调用时显示正确的场地,或者通过将控制器中的def索引更改为@venues = Venue.north / venue.south / venue.west。 I just cant figure a way to have all the venues displayed as a default but to then call each scope depending on which area is selected from the form. 我只是想办法让所有场地都显示为默认值,然后根据从表格中选择的区域调用每个范围。

I'm not bothered with using AJAX at this point I would just like to understand how it can be done in as simple a way as possible and without using sphinx/thinking_sphinx. 在这一点上我并不打算使用AJAX我只想了解如何以尽可能简单的方式完成它并且不使用sphinx / thinking_sphinx。

Model: 模型:

class Venue < ActiveRecord::Base
  belongs_to :user
  has_many :reviews
  belongs_to :area

  scope :north, where(:area_id => "2")
  scope :west, where(:area_id => "3")
  scope :south, where(:area_id => "4")
end

View: (venue index.html.erb) 查看:(场地index.html.erb)

<div class="filter_options_container">

  <form class="filter_form">
    <%= select("area", "area_id", Area.all.map {|a| [a.name, a.id] }) %>
    <input type="submit" value="Filter" />
  </form>
</div>

<div class="venue_partials_container">
  <%= render :partial => 'venue', :collection => @venues %>
</div>   

Controller: 控制器:

class VenuesController < ApplicationController

  def index
    @venues = Venue.all
  end
end

Any help is much appreciated. 任何帮助深表感谢。

You can find the Venues in your controller depending on whether an area is selected or not. 您可以在控制器中找到场地,具体取决于是否选择了区域。 You can modify the view to send the area name, instead of the area id(which might make it easier): 您可以修改视图以发送区域名称,而不是区域ID(这可能会使其更容易):


<%= select("area", "name", Area.all.collect(&:name)) %>

The controller would look something like this - 控制器看起来像这样 -


def index
  if (params[:area] && Area.all.collect(&:name).include?(params[:area][:name]))
     @venues = Venue.send(params[:area][:name].downcase)
  else
     @venues = Venue.all
  end
end

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

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