Starting with RJS - not working


#1

Hi,
I’m trying to work with RJS and folowed Cody F.'s tutorial
(http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates)
I’ve started by creating a new rails applications and executing
rake freeze_edge
rake rails:update:javascripts
rake rails:update:scripts

After that I’ve created a controller:
class DtestController < ApplicationController
def add
end
end

Created the files add.rhtml and add.rjs in views/dtest/ ,and
dtest.rhtml in views/layouts.
I’ve started my server and called my add action and nothing happends .
Can anyone explain me what I did wrong ?
My prototype version is 1.5.0_pre0

Thanks,
Ovidiu


#2

Created the files add.rhtml and add.rjs in views/dtest/ ,and
dtest.rhtml in views/layouts.

The tutorial says:

Rails now looks for templates with an extension of .rjs, in addition to
.rhtml and .rxml, so create a view named add.rjs for your controller and
add
the following code to it: bla, bla…

It doesn’t say anything about create a .rhtml view. It only says that
last
version of Rails search for templates .rjs too.
I haven’t used RJS but I think you only need .rjs view to follow the
tutorial.


#3

What I understand is that the .rjs page (which must have the same name
with the .rhtml page) is used – after – the page is rendered

From the tutorial>>>
Unlike conventional templates which are used to render the results of
an action, these templates generate instructions on how to modify an
already rendered page. This makes it easy to modify multiple elements
on your page in one declarative Ajax response. Actions with these
templates are called in the background with Ajax and make updates to
the page where the request originated from.

Ovidiu


#4

Hi,

I didn’t try RJS templates yet, but from what I understand if there are
both a .rhtml and a .rjs template, then rails picks up the .rhtml one.
Waht you’ll want to do in your add action is change the view produced by
a different action.

HTH,
Michael


#5

Ovidiu EFTIMIE wrote:

What I understand is that the .rjs page (which must have the same name
with the .rhtml page) is used – after – the page is rendered

No, it must not have the same name. There could potientially be several
actions wanting to change the rendered page in some way, so they have to
be distinct from the action that originally rendered the page. Give it a
try and make different actions for rendering and changing the page…


#6

I’ve added an index action
class DtestController < ApplicationController
def index
end
def add
end
end
and renamed the add.rhtml to index.rhtml.
Now it works well.
Acctually at the beggining id didn’t work at all . I’ve deleted the
ruby session from %TEMP% and then is started to work

Thanx,
Ovidiu


#7

Ovidiu EFTIMIE wrote:

What I understand is that the .rjs page (which must have the same name
with the .rhtml page) is used – after – the page is rendered

Nope, it works on an existing page.

so you call /controller/show/1, that triggers
views/controller/show.rhtml to display stuff.

You then issue an ajax call to /controller/add_list_item/1, that will
trigger /views/controller/add_to_list_item.rjs which returns javascript
to modify the existing (show) page.

Each action renders only one template, some are ®html, some are ®js.

Is this any clearer ?

A.


#8

Thanx Alan. Much clearer:)