无法找到[产品型号]没有ID:轨道4定制的控制器操作 [英] Couldn't find [MODEL] without an Id : Rails 4 custom controller action

查看:161
本文介绍了无法找到[产品型号]没有ID:轨道4定制的控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个JSON提要某些数据拉进图我有一个部分。我一直运行到找不到水坝没有ID,并没有对这样的回答似乎有此修复程序。我的PARAMS似乎经过精细路过,和我有点困惑。

routes.rb中

 规定resque /服务器
Rails.application.routes.draw做
  安装Resque ::服务器:在=> / resque

  根的家#指数
  获得关于,到:回家#关于

  资源:水坝,只有:[:索引:显示广告]做
      获得count_data,到:大坝#count_data
    资源:fish_counts,只有:[:索引:显示广告]
  结束

  资源:鱼
结束
 

在dams_controller.rb自定义操作:

 高清count_data
      @dam = Dam.includes(:fish_counts,:鱼).friendly.find(PARAMS [:ID])
      @fish_counts = FishCount.for_year(Date.today.year)。凡(dam_id =?,@ dam.id).limit(200).order(count_date:DESC)
      respond_to代码做|格式|
        format.json {
          渲染JSON:{:fish_counts => @fish_counts}
        }
      结束
  结束
 

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

为什么实例变量不拾取被传递给它的参数?而在这样的情况下,我将如何调试? 提高params.inspect 似乎已经展示了我的正确PARAMS正在经历。

感谢

修改

下面是控制台输出,当我搜索W / O friendly.find:

 入门GET/dams/3/count_data.json为:: 1,在2015年8月15日十八点11分04秒-0700
处理由DamsController#count_data为JSON
参数:{dam_id=>中3}
已完成404未找​​到在2毫秒(ActiveRecord的:0.0ms)

的ActiveRecord :: RecordNotFound(找不到水坝没有ID):
应用程序/控制器/ dams_controller.rb:15:在'count_data
 

解决方案

您在堆栈跟踪看到,有没有:ID的参数,可以,但是:dam_id,所以你应该使用这样的:

  @dam = Dam.includes(:fish_counts,:鱼).find(PARAMS [:dam_id])
 

I'm trying to create a JSON feed for certain data to pull into a graph I have in a partial. I keep running into "Couldn't find Dam without an Id", and none of the answers on SO seem to have the fix. My params seem to be passing through fine, and I'm a bit perplexed.

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

custom action in the 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

I have tried using the id instead of the friendly.find, and get the same result. I have success if I delete the @dam line and manually add in a @dam.id to the @fish_countsinstance.

Why is the instance variable not picking up the parameters being passed to it? And in situations like this, how would I debug? raise params.inspect seems to have just shown me that the proper params are passing through.

Thanks

edit

Here's the console output when I search 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'

解决方案

You see in the stack trace that there is no :id in params, but :dam_id, so you should use it like this :

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

这篇关于无法找到[产品型号]没有ID:轨道4定制的控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆