Skip to content
nesquena edited this page Mar 23, 2012 · 2 revisions

If you are having trouble with RABL and Rails 3.2, check out:

In a nutshell:

In issue 180, @carhartl 's comment and my comment addresses the issue of changing content types when you attempt to render a RABL partial in the middle of an existing HTML template. Otherwise rendering a RABL file directly render file: 'show.json.rabl' works just fine. (though it should be render 'show', formats: :json or just render 'show')

From @carhartl

In my case I found out that when using render template using rabl within another (html) layout, like for instance:

<%= render(template: 'users/current_user', formats: [:json], handlers: [:rabl]).html_safe %> any following call to render insists on json, so I had to make the format explicit again there:

<%= render(partial: 'foo/bar', formats: [:html]) %> Maybe this is just the way it works in Rails now...

From @databyte

I had a bitch of a time finding all the other renderings in my massive list of partials (client-side heavy, lots of template partials).

So I added this to my helpers:

def with_format(format, &block)
  old_formats = formats
  self.formats = [format]
  result = block.call
  self.formats = old_formats
  result
end
  • partially compiled from reading this and this.

Then I had a file like show.html.erb render this:

<%= render partial: "foo/bar", formats: :js, locals: { baz: baz } %>

Which then switches formats to JSON which bombs without the helper:

var test = <%= with_format(:json) { render(template: 'v1/recipes/show', formats: :json).html_safe } %>;

So the formats goes from :html to :json, renders out the RABL, and then back to :html

The formats: :json halts deprecation warnings.

GOOD NEWS 👏 👏 👏 ... There is a fix in 3.2 stable (unreleased): https://github.com/rails/rails/issues/5440 commit with tests!