2012-09-05 201 views
0

和不明白为什么我的代码不工作范围和选择

我旁边: people/index.html.erb =>

<% @people.each do |i| %> 
    <tr> 
     <td><%= i.id %></td> 
     <td><%= i.name %></td> 
     <td><%= link_to i.country_id, root_path(:country_id => i.country_id) %></td> 
     <td><%= i.state_id %></td> 
    </tr> 
    <% end %> 

people_controller:

def index 
    @people = Person.all 
    @people = @people.by_country_id(params[:country_id]) if params[:country_id].present? 
    end 

在person.rb

scope :by_country_id, lambda { |x| where(:country_id => x) } 

根:到=> '人#指数'

我想,当我点击的link_to,我收到的所有的人COUNTRY_ID

输出我有...本地主机:3000/COUNTRY_ID = 1001

NoMethodError in PeopleController#index 
undefined method `by_country_id' 

我在哪里错了?

+0

这不是工作如'@people = Person.all'给你'Person'对象的阵列,因此不能应用方法' by_country_id'如果你尝试这样做,它会给你一个错误undefine方法by_country_id为一个数组 – Salil

+0

是的,我忘了这个(在第一个答案评论) –

回答

0

变化

@people = @people.by_country_id(params[:country_id]) if params[:country_id].present? 

@people = Person.by_country_id(params[:country_id]) if params[:country_id].present? 
+0

我没有见过,你是正确的=)@people = Person 。更改为Person.order('created_at DESC')... thn =) –