从下拉列表索引页筛选结果 [英] Filter results on index page from dropdown

查看:151
本文介绍了从下拉列表索引页筛选结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是轨道3,我有两个型号,场地和每个地区有许多场馆区域,每个场馆都属于一个区域。

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.

我试图找到过滤由在下拉框中选择在同一页上什么区域上显示在场地索引会场记录的一个途径。

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.

我在模型中的作用域显示正确的场所时在轨道控制台或通过改变控制器的高清索引@venues = Venue.north / venue.south / venue.west调用。我只是不能计算办法都显示为一个默认的场地,而是然后调用哪个区域是从表单中选择每个范围视。

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

我不打扰使用AJAX在这一点上我只是想了解如何能在简单的方式,尽可能不使用狮身人面像/ thinking_sphinx来完成。

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.

型号:

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

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

View: (venue 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>   

控制器:

class VenuesController < ApplicationController

  def index
    @venues = Venue.all
  end
end

任何帮助是非常AP preciated。

Any help is much appreciated.

推荐答案

您可以找到场地在控制器根据一个区域是否被选中与否。 您可以修改,而不是区域ID的观点发送区域名称,(这可能更容易):

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



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

这篇关于从下拉列表索引页筛选结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆