0

I'm using Rails 3.2.16, rspec-rails 2.14.1 and jbuilder 2.0.2 and found out that rspec is not yielding the "json" object when rendering jbuilder partials using:

render :partial => 'my_partial'

where "my_partial" is a _mypartial.jbuilder file. It throws an error as if the "json" object used in the template was not there. My template does something very simple, like:

json.foo "bar"
json.bar "foo"

The error, in this case, would be: undefined method `foo' for nil:NilClass

On the other hand, when doing something like:

render 'template'

and inside 'template.jbuilder' we have something like:

json.partial! 'my_template'

it works fine.

Yes, I'm using "render_views" on my spec group.

Has anyone caught this error before?

1 Answers1

1

I've just found out what the problem was and I believe it would be very helpful to share it here, as it would have saved me a lot of time if someone had already said this...

The line which was throwing the error was this one:

json.style  Sass.compile(".homepage-hero{ #{ homepage.style } }")

The error was:

ActionView::Template::Error:
       undefined method `style' for nil:NilClass

I was thinking that the problem was at json.style, where the 'json' object could be nil, but the problem was at the homepage.style!

Lesson learned: using the same method name as the json field when rendering jsons is NOT a good idea...