How do I override the default RJS template?

Elliott Blatt wrote:

I would like to be
able to select a particular RJS file to excute based on the results of
some logic.

In the simplest terms, this is what I'd like to accomplish:

def reply
  if case == a
    [use reply_a.rjs]
  else
    [use reply_b.rjs]
  end
end

Now Rails has a convention that says, "if no render statement is
defined, look for and .rhtml or .rjs file to execute based on the name
of the action; i.e reply.rjs. I can seem to find a particluar command
that will allow me to specify which RJS to use.

The clue is in "if no render statement is defined..." :wink:

You need to use the 'render' method, and in particular the :action
option:

render(:action => :reply_a)

or

render(:action => :reply_b)

The documentation's here:

http://api.rubyonrails.org/classes/ActionController/Base.html#M000206

(Also, just curious, does anyone know of a way to do logic inside an RJS
template?)

An RJS template is just pure Ruby, so you can use the usual
if/elsif/end etc.

Chris

Elliott Blatt wrote:

Thanks, well done. I had tried, render :parial and render :template. I
didn't think of render :action for some reason.

Yeah, the :action option is a bit weirdly named. It makes you think
that it's going to execute the action you specify, but actually it just
renders the template by that name.

When I write render(:action => :wibble), I like to think of it as
telling Rails to "pretend that I'm in an action called 'wibble', and go
and find the default template for that action, following the normal
default rules". That's why it works equally well for RHTML/RJS/whatever
templates without specifying the extension, because Rails' default
behaviour is to find one of those templates given just the name of the
action.

Chris

harp wrote:

how do you pass parameteres to the RJS
template? all of the global variables that are in the action are passed
to the template? (@user, @page, @var) ? i can't seem to get a variable
into the RJS template ...

Yeah, that should work. If you do

@user = User.find(:first)

in your controller, your RJS template should be able to access that
instance variable as well:

page.alert(@user.name)

Same as any template. Can you post your code that's not working as
expected?

Chris