rendering a partial inside another using render_to_string and (:formats) in controller

482 views
Skip to first unread message

mathieu bougie

unread,
May 28, 2012, 9:24:13 AM5/28/12
to Ruby on Rails: Core
I want to clean my code by moving the duplicated lines :

partial = render_to_string( :partial => "#{file}", :formats =>
[:html] )

to the pagination_partials function.

=================================================
WORKING CODE:
=================================================

def render_format_search_partial(file, options={})
# TODO remove this line to use the one in the
pagination_partials function
partial = render_to_string( :partial => "#{file}", :formats =>
[:html] )

respond_to do |format|
format.json { render :json => pagination_partials(file,
partial, options), :status => 200 }
format.html
end
end

def render_json_search_partial(file, options={})
# TODO remove this line to use the one in the
pagination_partials function
partial = render_to_string( :partial => "#{file}", :formats =>
[:html] )
render :json => pagination_partials(file, partial,
options), :status => 200
end

def pagination_partials(file, partial, options={})
# TODO use this line instead of the duplicated lines above
#partial = render_to_string( :partial => "#{file}", :formats =>
[:html] )
pagination = render_to_string( :partial => 'shared/list/
will_paginate', :formats => [:html] )

hash = {:response => partial, :pagination => pagination}
options.each { |o, v| hash.merge(o => v) unless options.blank? }
hash
end

=====================================================
WORKING CODE:
=====================================================

def render_format_search_partial(file, options={})
partial = pagination_partials(file, options)

respond_to do |format|
format.json { render :json => partial, :status => 200 }
format.html
end
end

def render_json_search_partial(file, options={})
partial = pagination_partials(file, options)
render :json => partial, :status => 200
end

def pagination_partials(file, options={})
partial = render_to_string( :partial => "#{file}", :formats =>
[:html] )
pagination = render_to_string( :partial => 'shared/list/
will_paginate', :formats => [:html] )

hash = {:response => partial, :pagination => pagination}
options.each { |o, v| hash.merge(o => v) unless options.blank? }
hash
end

===============================================
EXPECTED WORKING CODE....but it doesnt work
===============================================

def render_format_search_partial(file, options={})
respond_to do |format|
format.json { render :json => pagination_partials(file,
options), :status => 200 }
format.html
end
end

def render_json_search_partial(file, options={})
render :json => pagination_partials(file, options), :status =>
200
end

def pagination_partials(file, options={})
partial = render_to_string( :partial => "#{file}", :formats =>
[:html] )
pagination = render_to_string( :partial => 'shared/list/
will_paginate', :formats => [:html] )

hash = {:response => partial, :pagination => pagination}
options.each { |o, v| hash.merge(o => v) unless options.blank? }
hash
end

============================================================
WILL_PAGINATE PARTIAL:
============================================================

<div class="pagination_wrapper">
<%= will_paginate @search_result, :page_links => false,
:previous_label => "#{image_tag
'icons/control-180.png', :class => "prev-button link-button link-
button-small"}",
:next_label => "#{image_tag 'icons/
control.png', :class => "next-button link-button link-button-small"}"
%>
</div>

============================================================
THE EVIL PARTIAL (users/user_list):
============================================================

<div class="list-table" data-source="<%=
search_enterprise_users_path(@enterprise, :format => :html) %>">
<%= render( :partial => 'user',
:collection => @search_results,
:spacer_template => 'shared/list/separator') ||
t('user.empty_result') %>
</div>

============================================================
THE ERROR:
============================================================

ActionView::MissingTemplate (Missing partial users/user_list with
{:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee, :haml]}.
Searched in:...

============================================================

I think something's wrong when I try to render a partial inside
another, I tried to remove the spacer_template in the render user
partial and i also try to add the formats => [:html] inside the render
block but without success....It will render properly only if I remove:

<%= render( :partial => 'user',
:collection => @search_results,
:spacer_template => 'shared/list/separator') ||
t('user.empty_result') %>

I don't know if some one can reproduce this issue but I'm interesting
to get some clue about this bug

Thx

Nick Sutterer

unread,
May 29, 2012, 9:25:55 AM5/29/12
to rubyonra...@googlegroups.com
Hi Mathieu,

if you nest partial rendering in Rails you cannot change the format since they manage global variables "for performance reasons". This is fixed in Cells which could replace your #pagination_partials - give it a try and hit me on the #cells IRC channel ;)
Reply all
Reply to author
Forward
0 new messages