# Starting with RJS - not working

**URL:** <https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795>\
**Category:** Rails\
**Created:** [March 14, 2006, 11:43am UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795 "2006-03-14T11:43:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [March 14, 2006, 11:43am UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795/1 "2006-03-14T11:43:45Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [March 14, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795/2 "2006-03-14T12:14:36Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [March 14, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795/3 "2006-03-14T12:26:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [March 14, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795/4 "2006-03-14T12:05:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [March 14, 2006, 12:32pm UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795/5 "2006-03-14T12:32:53Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [March 14, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795/6 "2006-03-14T14:05:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [March 14, 2006, 12:48pm UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795/7 "2006-03-14T12:48:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [March 14, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/starting-with-rjs-not-working/54795/8 "2006-03-14T14:17:51Z")

</div>

Thanx Alan. Much clearer:)
