Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

render_to_string partial format error in controller

I get the below error when running the below code on a controller. Please note the :formats=>[:json] in the error, even though :formats=>[:html] is passed into render_to_string

What am I doing wrong? Any ideas? Actually, the code below worked fine before, not sure what changes influenced this error. Rails version: 3.2.8

btw the template is definitely in place: loc/_search_details.html.erb

Bonus question: where can I find the api documentation showing what parameters can be passed to render_to_string and how it works?

Error: ActionView::MissingTemplate (Missing partial loc/search_details with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee]}.

  respond_to do |format|
    format.json { 
      @detail_str = render_to_string(:partial => 'loc/search_details', :layout => false, :formats=>[:html], :locals => {:beer_results => @beer_results})
      @list_str = render_to_string(:partial => 'loc/search_list', :layout => false,:formats=>[:html], :locals => {:beer_results => @beer_results})
      render :json => {:results => @results_hash, :result_details => @detail_str, :result_list => @list_str }

      }
  end
like image 304
vrepsys Avatar asked Dec 04 '12 22:12

vrepsys


2 Answers

See this question and this issue. Since you're making a JSON request your render_to_string is expecting your partial to be _search_details.json.erb. You can either provide an additional JSON partial, rename the partial or add this to the partial <% self.formats = [:json, :html] %>, or alternatively, try the workaround in the accepted answer to that question.

like image 148
David Avatar answered Nov 05 '22 08:11

David


What if you try

render_to_string(:partial => 'loc/search_details.html.erb', :layout => false, :locals => {:beer_results => @beer_results})

Or

with_format("html") { render_to_string(:partial => 'loc/search_details', :layout => false, :locals => {:beer_results => @beer_results}) }

And add the method

private
def with_format(format, &block)
  old_format = @template_format
  @template_format = format
  result = block.call
  @template_format = old_format
  return result
end

Source: How do I render a partial of a different format in Rails?

like image 4
mrbrdo Avatar answered Nov 05 '22 08:11

mrbrdo