undefined method `find_by' for nil:NilClass when using `model_class: false`

纵然是瞬间 提交于 2019-12-24 06:20:14

问题


undefined method 'find_by' for nil:NilClass

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

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

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.

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.

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)

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

  • 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

回答1:


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"
}

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



来源:https://stackoverflow.com/questions/37872288/undefined-method-find-by-for-nilnilclass-when-using-model-class-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!