Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a Missing Partial error occur occasionally when the file exists?

Tags:

While running Cucumber tests, from time to time it will fail intermittently. The error is:

*** ActionView::Template::Error Exception: Missing partial items/_fields with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml]}. Searched in:   * "/my/path/to/application/app/views"   * "/my/path/to/rvm/gems/ruby-2.1.5/gems/kaminari-0.16.3/app/views"   * "/my/path/to/rvm/gems/ruby-2.1.5/gems/devise-3.5.4/app/views"  

Yes, the partial file does exist in /my/path/to/application/app/views/items/_fields.html.haml

The kicker is that sometimes the test will pass, other times it will fail. I am running on a Red Hat 5 machine.

So to debug this, I decided to throw in a begin/rescue.

.fields   - begin     = render partial: 'items/fields', locals: {f: f}   - rescue Exception => e     = byebug    - if f.can_modify?(:object_a)     = render partial: 'layouts/object_a_field', locals: {f: f, field: :object_a}    - else     .field#object_a       = render partial: 'layouts/attribute', locals: {label: 'Object A', value: f.object.object_a_id ? f.object.object_a_number : 'Not Assigned'}      .field#name     - if f.can_modify?(:name)       = f.label :name       = f.text_field :name, {class: 'inputbox large_field', readonly: f.cannot_modify?(:name)}       .smltxt (short description of the item)     - else       = render partial: 'layouts/attribute', object: f.object, locals: {field: :name} 

For those curious what's in the partial items/fields:

.field   - if f.can_modify?(:name)     = f.label :name, 'Item'     = f.text_field :name, {class: 'inputbox uppercase', maxlength: 16, readonly: !f.object.identifier.new_record?}     .smltxt (character identifier)   - else     = render partial: 'layouts/attribute', object: f.object, locals: {field: :name, label: 'Item'} 

When it passes, I obviously don't hit the byebug, but when it fails, I do! Which allows me to play with it in a Rails Console type environment.

So I decided to run that specific command:

(byebug) render partial: 'items/fields', locals: {f: f} 

And I get:

*** ActionView::Template::Error Exception: Missing partial items/_fields with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml]}. Searched in:   * "/my/path/to/application/app/views"   * "/my/path/to/rvm/gems/ruby-2.1.5/gems/kaminari-0.16.3/app/views"   * "/my/path/to/rvm/gems/ruby-2.1.5/gems/devise-3.5.4/app/views"  

So I thought I'd remove the locals to see if anything changed:

(byebug) render partial: 'items/fields' 

And I get:

*** ActionView::Template::Error Exception: undefined local variable or method `f' for #<#<Class:0x0000002a907610>:0x00000027beae48> 

So obviously it's now magically finding the partial, and it knows that it is missing a local variable f.

Update 1

I added the rest of the view that is calling the partial above for clarity.

In an attempt to troubleshoot, I also took the contents of items/_fields.html.haml and placed it where I had the render partial: 'items/fields', locals: {f: f}... then the test passed, which means that the other partials in the file didn't have an issue either. So it doesn't seem like it could be related to the content of the file.

Update 2

It seems by adding the @javascript tag to our very first Cucumber feature often fixes this issue. Somehow having the browser loaded before the other tests, improves the chances of it passing. Makes no sense to me, maybe someone else has an idea about this?

Update 3

It has been discovered today that the error is not related to Cucumber at all. The same error was found in production. The web interface showed a Missing Partial error, but for a file that did indeed exist. After restarting the Passenger server with touch tmp/restart.txt and then the error went away. So it's still very intermittent.

Update 4

I added the content of the partial that seems to be missing randomly.

How would I go about discovering why Rails cannot find a partial that is actually there?

like image 920
ardavis Avatar asked Feb 01 '16 14:02

ardavis


1 Answers

This error usually happens when the format of the request is different and there is no template that can respond to it example format.js, format.html etc. The request will only work if the template items/_fields.html.haml is a it will only be correct if the request format is html

I think you can rename your file to items/_fields.haml if you want to use to respond it any request format.

like image 54
aliibrahim Avatar answered Dec 27 '22 03:12

aliibrahim