找不到没有 Id 的 [MODEL]:Rails 4 自定义控制器操作

Couldn't find [MODEL] without an Id : Rails 4 custom controller action

我正在尝试为某些数据创建一个 JSON 提要,以将其提取到我的部分图表中。我将 运行 保留为 "Couldn't find Dam without an Id",SO 上的 none 答案似乎已修复。我的参数似乎通过得很好,我有点困惑。

routes.rb

require "resque/server"
Rails.application.routes.draw do
  mount Resque::Server, :at => "/resque"

  root 'home#index'
  get 'about', to: 'home#about'

  resources :dams, only: [:index, :show] do
      get 'count_data', to: 'dams#count_data'
    resources :fish_counts, only: [:index, :show]
  end

  resources :fish
end

dams_controller.rb 中的自定义操作:

    def count_data
      @dam = Dam.includes(:fish_counts, :fish).friendly.find(params[:id])
      @fish_counts = FishCount.for_year(Date.today.year).where("dam_id =?",  @dam.id).limit(200).order(count_date: :desc)
      respond_to do |format|
        format.json {
          render json: {:fish_counts => @fish_counts}
        }
      end
  end

我尝试使用 id 而不是 friendly.find,并得到相同的结果。如果删除 @dam 行并手动将 @dam.id 添加到 @fish_counts 实例,我就会成功。

为什么实例变量没有获取传递给它的参数?在这种情况下,我将如何调试? raise params.inspect 似乎刚刚向我展示了正确的参数正在通过。

谢谢

编辑

这是我搜索 w/o friendly.find 时的控制台输出:

Started GET "/dams/3/count_data.json" for ::1 at 2015-08-15 18:11:04 -0700
Processing by DamsController#count_data as JSON
Parameters: {"dam_id"=>"3"}
Completed 404 Not Found in 2ms (ActiveRecord: 0.0ms)

ActiveRecord::RecordNotFound (Couldn't find Dam without an ID):
app/controllers/dams_controller.rb:15:in `count_data'

你在堆栈跟踪中看到参数中没有:id,但是:dam_id,所以你应该这样使用它:

@dam = Dam.includes(:fish_counts, :fish).find(params[:dam_id])