当使用`model_class:false'时,未定义的方法'find_by'为nil:NilClass [英] undefined method `find_by' for nil:NilClass when using `model_class: false`

查看:95
本文介绍了当使用`model_class:false'时,未定义的方法'find_by'为nil:NilClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

undefined method 'find_by' for nil:NilClass

提取的来源(围绕第38行):

Extracted source (around line #38):

def update
  respond_to do |format|
    if @tag.update(tag_params)
      format.html { redirect_to @tag, notice: 'Tag was successfully updated.' }
      format.json { render :show, status: :ok, location: @tag }
    else
      format.html { render :edit }
      format.json { render json: @artefact.errors, status: :unprocessable_entity }
    end
  end
end

我正在修改脚手架生成器以将:relations自动处理为has_many,并将迭代器添加到用户端. 由于某些原因,所有具有have_many关系的生成的类都存在此问题.

I am working on modifying the scaffold generators to automatically process :relations as has_many, and add iterators to the user-side. For some reason, all the generated classes with have_many relationships have this issue.

在创建或更新时,不会创建(/修改)记录,而是显示此错误.

On create or update, the record is not created (/modified), and instead this error is shown.

为了很好的衡量,以下是表单参数,即使我放在这里也没关系:

For good measure, here are the form params, even though it doesn't matter what I put there:

{
 "utf8"=>"✓",
         "_method"=>"patch",
         "authenticity_token"=>"",
         "tag"=>{"name"=>"Downloadable",
         "normalized_name"=>"downloadable", 
         "tagged"=>[""]},
 "commit"=>"Update Tag",
 "id"=>"1604f0d6-4b8c-4305-8858-f2db53b1947d"
}

有关的东西,但我不确定为什么:

Something that relates, but I'm not sure why:

def tag_params
  params.require(:tag).permit( :name, :normalized_name, tagged: [])
end

如果我删除了对tagged拥有数组的功能,它将停止该错误.当然,由于它是has_many,所以我想要一个数组.

If I remove the ability to have an array for tagged, it stops the error. Of course, since it's a has_many, an array is what I want.

编辑:来自 Mukesh 的提示让我看着before_action

Edit: The tip from Mukesh had me looking at before_action which does call

def set_tag
  @tag = Tag.find(params[:id])
end

编辑:在处理其他项目一段时间后,我又回到了这一点,但它仍然让我感到困惑.

Edit: I came back to this after some time working on other projects, and it still stumps me.

这是完整的输出(请注意: Pravesh Khatri 的建议并没有改变错误):

Here's the complete output (Note: the suggestion from Pravesh Khatri didn't change the error):

Started PATCH "/tags/e4c42c16-f547-4cc4-8d50-cbf85650563a" for 127.0.0.1 at 2016-07-16 22:23:55 -0300
Processing by TagsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"b5EuyanawfG7lSCTt6aC1jAn8k66epnr5//KeeiC7GkhW6n4BWWJKeHW4OB87N2YGPwuJG2ynZwlzhWcvbQepQ==", "tag"=>{"name"=>"Tag2", "tagged"=>[""]}, "commit"=>"Update Tag", "id"=>"e4c42c16-f547-4cc4-8d50-cbf85650563a"}
 Tag 4ms MATCH (n:`Tag`) WHERE (n.uuid = {n_uuid}) RETURN n ORDER BY n.uuid LIMIT {limit_1} | {:n_uuid=>"e4c42c16-f547-4cc4-8d50-cbf85650563a", :limit_1=>1}
 Tag#tagged 3ms MATCH (tag6) WHERE (ID(tag6) = {ID_tag6}) MATCH (tag6)<-[rel1:`CONCEPTUAL_TAG`]-(result_tagged) DELETE rel1 | {:ID_tag6=>6}
Completed 500 Internal Server Error in 35ms



NoMethodError (undefined method `find_by' for nil:NilClass):

app/controllers/tags_controller.rb:37:in `update'
  Rendering /Users/josh/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /Users/josh/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /Users/josh/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.1ms)
  Rendering /Users/josh/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /Users/josh/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.5ms)
  Rendering /Users/josh/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /Users/josh/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.7ms)
  Rendered /Users/josh/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (90.9ms)

它在一个相关项目中咬住了我,我发现它发生在以下时间:

It bit me in a related project, and I found that it's happening when:

  • 关系为has_manymodel_classfalse
  • 控制器允许参数作为数组,例如:tagged => []
  • 提交的表单可以创建一个新节点,或者在提供该参数的同时更新现有节点
  • The relationship is has_many and a model_class of false
  • The controller permits the parameter as an array, such as :tagged => []
  • The form is submitted that either creates a new node, or updates an existing node while providing that parameter

推荐答案

在您的控制器中...检查在更新之前正在执行的操作(控制器上的顶部) 并检查您的参数

In your controller ... check action which is executing before update (Top on controller) And check your params

{
 "utf8"=>"✓",
         "_method"=>"patch",
         "authenticity_token"=>"",
         "tag"=>{"name"=>"Downloadable",
         "normalized_name"=>"downloadable", 
         "tagged"=>[""]},
 "commit"=>"Update Tag",
 "id"=>"1604f0d6-4b8c-4305-8858-f2db53b1947d"
}

是否具有该操作..(在find_by中)

It has or not.. which is required in that action (in find_by)

这篇关于当使用`model_class:false'时,未定义的方法'find_by'为nil:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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