mathieu bougie
2012-May-28 13:24 UTC
rendering a partial inside another using render_to_string and (:formats) in controller
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Nick Sutterer
2012-May-29 13:25 UTC
Re: rendering a partial inside another using render_to_string and (:formats) in controller
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 ;) On Monday, May 28, 2012 3:24:13 PM UTC+2, mathieu bougie wrote:> > 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 >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/VKve6jLRpSYJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.