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

在人.rb

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

根 :to => 'people#index'

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

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

NoMethodError in PeopleController#index 
undefined method `by_country_id'

我错在哪里?

4

1 回答 1

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?
于 2012-09-05T07:16:04.100 回答