Stupid WTF! ActionView::MissingTemplate Exception: Missing template users/_user.erb
If you've spent much time working with Ruby on Rails and more especially if you've done anything with json you might have run across errors like this:
ActionView::MissingTemplate Exception: Missing template users/_user.erb
I was having this problem and doing a lot of cursing which is common when I stay up and write code until 2am which I know I should do and I also know that I shouldn't write really long run on sentences but I do it anyway.
This error is usually the result of some code that looks like this:
format.json do @user_html = render_to_string( :partial => 'users/user', <img src='http://www.justinball.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> bject => @user ) render :json => { :success => true, :user_html => @user_html } end
The cause of this error is that the format of the current request is :json. There isn't an _user.json.erb template so Rails tries to find an _user.erb file. That doesn't exist either and boom you spend the night sounding like a sailor.
Now I don't pretend to be an expert but I have been accused of being a hack. I solve the problem by changing the template format:
format.json do @template.template_format = :html @user_html = render_to_string( :partial => 'users/user', <img src='http://www.justinball.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> bject => @user ) render :json => { :success => true, :user_html => @user_html } end
The key is the addition of:
@template.template_format = :html
Don't yell at me if it breaks. I'm open to better suggestions but for now this does work and now my I can find other reasons to stay up late.
More from jbasdf
- A copy of ApplicationController has been removed from the module tree but is still active! And can’t dup NilClass
- My Wife the Blog Stalker
- Interesting testing issue with Rails 2.3.3
- to_json gives TypeError Exception: wrong argument type Hash (expected Data)
- Do the economics really work out on this?
-
jbasdf
-
shripad
-
Patelakash_a
-
Christoph
-
MicahWedemeyer
-
Louis
Justin Ball is a software consultant and entrepreneur with a passion for Ruby. He evolved from a C++ and .Net monkey into a python programmer and finally found Ruby. In the rare moments when he isn't writing code, talking about code or measuring his code productivity in profanity per hour, you can find him on his bike in the mountains or on the roads surrounding Cache Valley. 









