# Common RJS

**URL:** <https://www.ruby-forum.com/t/common-rjs/129314>\
**Category:** Rails\
**Created:** [February 16, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/common-rjs/129314 "2008-02-16T17:03:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [February 16, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/common-rjs/129314/1 "2008-02-16T17:03:25Z")

</div>

I want to have some RJS executed whenever an xhr request is made. My  
initial thought was an “application.rjs” might do this, however that  
does not seem to be the case.

Is there a way to set up some RJS that is executed whenever RJS is  
fired? I have code that I don’t want to repeat all over the place?

Failing the above. I thought adding a helper function the application.rb  
and calling that from each .rjs file. However this does not work either.  
Is there a way to call a method defined in applicaiton.rb from any .rjs  
file?

thanks!

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [February 17, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/common-rjs/129314/2 "2008-02-17T20:34:43Z")

</div>

in your controller try something like this.

format.js { render :template =\> “shared/error.js.rjs” }

John I

On Feb 16, 10:03 am, John L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [February 18, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/common-rjs/129314/3 "2008-02-18T13:18:40Z")

</div>

Not sure I understand you John - can you clarify what you mean by

> in your controller try something like this.
> 
> format.js { render :template =\> “shared/error.js.rjs” }

Are you saying put something in my application controller (if so,  
where?) or in the controller action method that is being fired before  
the controller/action.rjs file is executed?

If it’s the latter then that would mean repearing it on every action  
that has RJS.

What I’m really looking for is if Rails has a way of invoking a common  
RJS file on every RJS action.

My reasoning for this is there are “core” tasks that I’d like to do  
using Ajax, such as showing/hiding my flash bar depending on whether  
there is a message to display.

Many thanks for your help.

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [February 18, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/common-rjs/129314/4 "2008-02-18T13:57:01Z")

</div>

On 16 Feb 2008, at 16:03, John L. wrote:

> and calling that from each .rjs file. However this does not work  
> either.  
> Is there a way to call a method defined in applicaiton.rb from  
> any .rjs  
> file?

put this in your application.rb (or any appropriate helper file)

def some\_common\_stuff  
update\_page do |page|  
page.some\_rjs\_stuff  
…  
end  
end

Then in your rjs files you can do

page \<\< some\_common\_stuff

Fred

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [February 18, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/common-rjs/129314/5 "2008-02-18T22:25:48Z")

</div>

> put this in your application.rb (or any appropriate helper file)
> 
> def some\_common\_stuff  
> update\_page do |page|  
> page.some\_rjs\_stuff  
> …  
> end  
> end
> 
> Then in your rjs files you can do
> 
> page \<\< some\_common\_stuff
> 
> Fred

Here is what I tried. I put this method within my application.rb :

def rjs\_update\_flash  
put “rjs update flash”  
update\_page do |page|  
unless flash.empty?  
if flash[:error]  
page.replace\_html(‘alert\_panel\_message’, “

![!](https://www.ruby-forum.com/images/warning_sign.jpg)#{flash[:error]}

”)  
elsif flash[:notice]  
page.replace\_html(‘alert\_panel\_message’, “

#{flash[:notice]}

”)  
end  
page.show(‘alert\_panel’)  
else  
page.hide(‘alert\_panel’)  
end  
end  
end

And, in my rjs file:

page \<\< rjs\_update\_flash

My log shows a 500 internal error.

I also tried putting the method in application\_helper.rb but that didn’t  
work either.

Have I done something wrong?

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [February 19, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/common-rjs/129314/6 "2008-02-19T16:54:55Z")

</div>

This was modified from peepcodes RJS screencast ( I updated it for  
rails 2)  
[http://peepcode.com/products/rjs-templates](http://peepcode.com/products/rjs-templates)

## They use a task list for the demo. In the tasks\_controller

# POST /tasks

# POST /tasks.xml

def create  
@task = Task.new(params[:task])

```
respond_to do |format|
  if @task.save
    calculate_totals
    flash[:notice] = 'Task was successfully created.'
    format.html { redirect_to(@task) }
    format.xml { render :xml => @task, :status

```

=\> :created, :location =\> @task }  
format.js  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @task.errors, :status  
=\> :unprocessable\_entity }  
format.js { render :template =\> “shared/error.js.rjs” }  
end  
end  
end

# PUT /tasks/1

# PUT /tasks/1.xml

def update  
@task = Task.find(params[:id])

```
respond_to do |format|
  if @task.update_attributes(params[:task])
    flash[:notice] = 'Task was successfully updated.'
    format.html { redirect_to(@task) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml { render :xml => @task.errors, :status

```

## =\> :unprocessable\_entity } format.js { render :template =\> “shared/error.js.rjs” } end end end

When ever something is not saved or updated VIA AJAX if fires the  
shared/error.js.rjs file  
I gues this could be DRYed up.

the shared/error.js.rjs file just has  
page.alert “An error happened!”

HTH

John Ivanodd

On Feb 18, 3:25 pm, John L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [February 19, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/common-rjs/129314/7 "2008-02-19T17:34:30Z")

</div>

On 18 Feb 2008, at 21:25, John L. wrote:

> work either.
> 
> Have I done something wrong?

It definitely should be application\_helper (my bad, not quite typing  
what i was thing). It would be awfully helpful to know what the error  
was (check the log files)

Fred

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [February 20, 2008, 1:06am UTC](https://www.ruby-forum.com/t/common-rjs/129314/8 "2008-02-20T01:06:30Z")

</div>

> > page \<\< rjs\_update\_flash  
> > My log shows a 500 internal error.  
> > I also tried putting the method in application\_helper.rb but that  
> > didn’t  
> > work either.

This does work (on Rails 2.0.2 at least):

in application\_helper.rb:

module ApplicationHelper  
def display\_flash(flash)  
page.replace\_html(‘FlashPane’, flash[:notice])  
end  
end

in your “create.js.rjs” file:

page.display\_flash(flash)

You need to pass the flash hash in specifically. I am using this now,  
it works really nicely - not just for flash, you can put any RJS in  
there you want.

Mikel

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar
