范围和选择

时间:2012-09-05 07:12:55

标签: ruby-on-rails scope

并且无法理解为什么我的代码不起作用

我有下一个: 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

in person.rb

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

root:to =&gt; '人#索引'

我想,当我点击link_to时,我收到了所有有country_id的人

在输出中我有... localhost:3000 /?country_id = 1001

NoMethodError in PeopleController#index 
undefined method `by_country_id'

我错了?

1 个答案:

答案 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?