未定义的方法`find_by' for nil:使用`model_class:false`时的NilClass

时间:2016-06-17 02:41:22

标签: ruby-on-rails neo4j.rb

undefined method 'find_by' for nil:NilClass

提取的来源(第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

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

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

为了更好的衡量,这里有形式参数,即使它放在那里并不重要:

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

有关的事情,但我不确定原因:

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

如果删除了tagged数组的功能,则会停止错误。当然,因为它是has_many,所以我想要一个数组。

修改:来自Mukesh的提示让我看着before_action打电话

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

编辑:我在其他项目工作一段时间后回到了这里,它仍然让我感到困惑。

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

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)

编辑:我在相关项目中找到了我,我发现它发生在以下情况:

  • 关系为has_manymodel_classfalse
  • 控制器允许参数作为数组,例如:tagged => []
  • 提交表单以创建新节点,或在提供该参数时更新现有节点

1 个答案:

答案 0 :(得分:1)

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

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

它有或没有...在该动作中需要(在find_by中)