# Ruby and rjs

**URL:** <https://www.ruby-forum.com/t/ruby-and-rjs/133054>\
**Category:** Rails\
**Created:** [March 27, 2008, 7:17am UTC](https://www.ruby-forum.com/t/ruby-and-rjs/133054 "2008-03-27T07:17:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [March 27, 2008, 7:17am UTC](https://www.ruby-forum.com/t/ruby-and-rjs/133054/1 "2008-03-27T07:17:17Z")

</div>

Hi all…

Can we write ruby code directly in an rjs file?

I have to replace a div with

\<%= text\_field “ciclass”, “name”, “size” =\> 20, “value” =\> @class\_name,  
:readonly =\> “readonly” %\>  
\<%= hidden\_field “ci”, “ci\_class”, “value” =\> @class\_id %\>

For doing this i’ve to write the above code in a partial file and  
update the div as follows in the rjs file as shown below:

page.replace\_html “div\_name”, :partial =\> “partial\_file\_name”

I was looking for a simpler way so that i can avoid writing a partial  
file for the above 2 lines.

Is there any way to write the above code directly in rjs file?

Please help

Thanks  
Regards  
Suneeta

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [March 27, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/ruby-and-rjs/133054/2 "2008-03-27T12:22:51Z")

</div>

Suneeta Km wrote:

> Can we write ruby code directly in an rjs file?

rjs files themselves might be overkill. I have never needed to do  
anything but  
this, straight out of a controller:

render :update do |rjs|  
rjs.replace\_html “div\_name”, :partial =\> “partial\_file\_name”  
end

(The variable that’s usually called ‘page’ I renamed to ‘rjs’, because  
the  
Interthing already has too danged many variables in it called ‘page’.)

And I would not hesitate to write a two-line partial. But you might get  
by with  
an inline:

rjs.replace\_html “div\_name”, :inline =\>  
“\<%= text\_field ‘ciclass’, ‘name’, :size =\> 20,  
:value =\> #{@class\_name},  
:readonly =\> “readonly” %\>  
\<%= hidden\_field ‘ci’, ‘ci\_class’, :value =\> #{@class\_id} %\>”

> Is there any way to write the above code directly in rjs file?

Instead of stomping out an entire div (and possibly twitching the user’s  
keyboard focus) can’t you just update those fields’ values directly?

–  
Phlip

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [March 27, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/ruby-and-rjs/133054/3 "2008-03-27T12:45:43Z")

</div>

Phlip wrote:

> Instead of stomping out an entire div (and possibly twitching the user’s  
> keyboard focus) can’t you just update those fields’ values directly?
> 
> –  
> Phlip

Hi…

I solved the problem by writing a 2 line partial file and rendering it  
in rjs file.

I tried to update those field values alone directly. I have some other  
fields in the form. So when i select their value, the above updated  
value is not shown.

So i did it this way

Thanks  
Suneeta
