提供category_id时,过滤记录会导致未定义的方法

时间:2015-06-03 09:28:04

标签: ruby-on-rails-4 filtering

我的应用程序中的过滤功能有问题。请在这里输入: http://leszczyna.wzks.uj.edu.pl/12_stolarski/events_for_seniors/pl/events?utf8=%E2%9C%93&search=Warszawa&date=02-06-2015

搜索工作正常,但当我尝试按类别(左侧面板)进行过滤时,我收到错误:

  

未定义的方法`category_id'

此外,如果您在category_whatever的链接中更改category_id,则错误消失(但过滤仍然无效)。 有人知道错误出现的原因吗?

以下是代码:

类别模型

class Category < ActiveRecord::Base
  has_many :events
end

活动模式

class Event < ActiveRecord::Base
  include Filterable
  belongs_to :city
  belongs_to :category
end

事件控制器(索引操作)

def index
  if params[:search] && params[:date] && @city
    date = params[:date].to_date
    @events = @city.events.filter(
        params.slice(
            :category_id, 
            :start_date, 
            :cost)
    )
    @categories = get_categories(@events)
  end
end

过滤funtcion (来自http://www.justinweiss.com/blog/2014/02/17/search-and-filter-rails-models-without-bloating-your-controller/):

module Filterable
extend ActiveSupport::Concern

module ClassMethods
  def filter(filtering_params)
    results = self.where(nil)
    filtering_params.each do |key, value|
      results = results.public_send(key, value) if value.present?
    end
    results
  end
end
end

类别面板

<div class="panel-body">
<ul>
  <% @categories.each do |category| %>
      <li><%= link_to category.name, events_path(
                              :search => params[:search],
                              :date => params[:date],
                              :category_id => category.id
                      )
      %></li>
  <% end %>
</ul>
</div>

1 个答案:

答案 0 :(得分:1)

您的搜索尝试:

 
    function callGetDomain() 
    { 
        var res1=$("#domainname").val();
        var res2=$("#name").val();
        var name=res1+res2; if(res1!="Domain Name")  {           
        location.href="https://www.google.lk/search?q=test"; }
        else { alert("Please Specify Domain Name"); }
    } 

这是最好的工作。你和其他参数如cost有相同的结果。你的目标是使用这样的东西:

 
<div class="search_domaim">
<div class="title_text">
 <h2>Register This Domain</h2></div>
<div class="search_field">
<form action="" method="get">
<div class="search_input">
<input name="domainname" type="text" class="textbox" id="domainname" onfocus="if (value == 'Domain Name') {value =''}" onblur="if (value == '') {value = 'Domain Name'}" value="Domain Name"></div>
<div class="search_drop">
<select name="name" id="name">
 <option value=".com">.com</option>
 <option value=".in">.in</option>
</select>
</div>
<div class="search_btn">
<!--<input name="" type="image" src="images/domain_search_btn.png" />-->
<input type="submit" value="Search" class="domain_submit" onclick="callGetDomain();">
</div>
<div class="clear"></div>
</form>
</div>
<div class="clear"></div>
</div>

过滤器doenst为关系工作。

最好的

编辑:

解决的一种方法:

@city.events.category_id

然后你可以打电话

@city.events.where(category_id: category_id)