WideFix tech post

Rails 3 does not render partial for specific format - resolving issue

Problem

For example you make ajax request with json format to the server. And assume we have to render some partial in the controller or view and return it in json object. In the view you may have code like this:

  • Rails3 view
<%= {:form => render('form')}.to_json %>

In the Rails 2 you could pass format as option in reder function and it fixed problem:

  • Rails2 view
<%= {:form => render(:partial => 'form', :format => :html)}.to_json %>

But in Rails 2 this is not working. But there is a fix this issue and it merged to the master. So, in rails 4 it indeed will work and signature for method will be like this:

  • Rails4 view
<%= {:form => render(:partial => 'form', :formats => [:html])}.to_json %>

To enable this feature in Rails 3 you can make monkey patch: just create new file config/initializers/renderer.rb in your application and paste there this code:

  • Mokeypatch for Rails3
class ActionView::PartialRenderer
  private
  def setup_with_formats(context, options, block)
    formats = Array(options[:formats])
    @lookup_context.formats = formats | @lookup_context.formats
    setup_without_formats(context, options, block)
  end

  alias_method_chain :setup, :formats
end
Are you seeking assistance with Ruby on Rails development?

Read also