Specify RJS Templates

Hey all,

Is there a way to specify which RJS template to use for
an action? Similar to render :template => “template”. I’ve got an
action that I need to have render different templates within the same
action. Due to a front-end Mongrel plugin I don’t want to have an
action for each template like you would normally.
As far as I can tell, you can only have one RJS template for an action, is that true?


if foo
  render :action => 'one.rjs'
  render :action => 'another.rjs'

Actually, you can use extensions when you do usual render:
  render :action => 'foo.rhtml'

If no extension given, it deduces it. In fact, the :action argument doesn't
mean that it will somehow render an action. It means that it will use
template with given name that should be located in template folder for
current controller.

Matt, I would recommending reading and completing the tutorials of
"RJS Templates for Rails" by Cody Fauser because it provides a wealth
of information on RJS. BTW, the answer to your question is answered
on page 4.

Good luck,


Thanks all…

I’ve got Cody’s book, but that part apparently didn’t stick :). And I did use render :template => “rjstemplate”, and I got an error thrown that it couldn’t find rjstemplate.rhtml, so it appeared that the render was automatically extending the filename to .rhtml.

I’ll check out Cody’s solution…


Rails searches for templates in some order. I believe, it first tries to find .rhtml file, then, perhaps, .rxml and .rjs.

Concerning your problem: make sure you are using latest Rails release.


I used your original render :action => “template.rjs” option, and that works fine. I’m on Edge, but when I try render :template => “template” or render :template => "template.rjs
" assert_existence_of_template_file throws a MissingTemplate error because it’s looking for the wrong file type (rhtml).

For some reason Cody’s book has dropped out of Safari and my bookshelf, so I can’t verify what his method was. I’ve sent O’Reilly an email, but they haven’t fixed it yet.


I've got Cody's book, but that part apparently didn't stick :). And I did
use render :template => "rjstemplate", and I got an error thrown that it
couldn't find rjstemplate.rhtml, so it appeared that the render was
automatically extending the filename to .rhtml.

render :template => "rjstemplate", :type => :rjs


Hmmm… I’m looking @ the code for action_controller/base and it seems that :type only gets taken into account when rendering inline templates, not file-based ones. Suffice it to say, I got the same file-type extension error I did before, where it puts .rhtml onto the end instead of .rjs.

render :action => “template.rjs” works because render_action takes into account the .rjs extension and renders it without layout by default… It seems like at this point the best option is the render :action => "

Thanks, though!


Difference between render :action => and render :template => is that template is searched relative to TEMPLATE ROOT (which is RAILS_ROOT/app/views by default). So to use render :template => you should supply proper path, e.g. render :template => ‘mycontroller/template.rjs’.

Hope that helps.

Yep, makes perfect sense. I confirmed that by looking at the ActionController::Base source… It does a simple check for existence to see if the file exists based solely on the path passed to :template, and if it doesn’t then it calls @
template.send(:full_template_path, template_name, ‘rhtml’) to get the file name. Interestingly, the last parameter on full_template_path is the file type. So, I wonder if a simple fix to this problem would be to have it also accept the :type parameter as was mentioned previously on this thread.

Then you could just do render :template => “filename”, :type => :rjs and it would actually work.

Either way, it seems that :action is the most concise solution.