Christaan Van den Poel
2010-Jun-24 08:19 UTC
Rails3: render_to_string of a .html.erb when request is a json request raises MissingTemplate error
I''ve the following situation (which actually works in Rails 2.3.8 and
this is a result of the migration of our app to rails3):
I have a partial which is rendered a few times (imagine tr''s in a
table). On the clientside there''s a script that does a json request
for a specific row (jQuery + ajax dataType: ''json'' and type:
''POST'')
In the controller I do:
render :json => { :success => true, :p => render_to_string(:partial
=>
''some_partial'') }
Then the ajax result handler replaces the row with the data from the
json result.
This is 100% functional code in rails 2.3.8, but raises
MissingTemplate exception:
ActionView::MissingTemplate (Missing partial employees/
employee_list_row_data with
{:handlers=>[:haml, :builder, :erb, :rjs, :rhtml, :rxml],
:formats=>[:json, :js, :"*/
*"], :locale=>[:en, :en]} in view paths ...
So it seems that the requested formats are used to search for the
templates.
First question: is this change deliberate? Is it now necessary to
have templates in the form of: "xxx.erb" (without the
''html'') so it
can be used for requests in other formats? Removing the
''.html'' part
of the file fixes this problem. I just want to know whether this is as
expected or a bug.
Second question: when I look at the query (actionpack/lib/action_view/
template/resolver.rb line 71) it is like this: app/views/layouts/
tags{.en,.en,}{.json,.js,.*/*,}
{.haml,.builder,.erb,.rjs,.rhtml,.rxml,} You can see it uses the
request.format ''*/*'' also in the Dir[query]. Is this possibly
a bug?
Because in (/home/cvp/myprojects/github/rails@github/actionpack/lib/
action_view/template/resolver.rb line 152 there''s the following code
# Overload formats= to reject [:"*/*"] values.
def formats=(values)
if values && values.size == 1
value = values.first
values = nil if value == :"*/*"
values << :html if value == :js
end
super(values)
end
I see there''s specific code to handle ''*/*'' formats
but only when this
is the only requested format. Is this possibly a bug?
TIA
C+++
--
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.
Apparently Analagous Threads
- Re: Train wreck getting render_to_string to work from foreign controller [SOLVED]
- This is not the error: ActionView::MissingTemplate
- rendering a partial inside another using render_to_string and (:formats) in controller
- Rails 3.0 + haml render partial ActionView::MissingTemplate
- controller not rendering existing js.erb file
