# RJS happening too fast?

**URL:** <https://www.ruby-forum.com/t/rjs-happening-too-fast/130404>\
**Category:** Rails\
**Created:** [February 28, 2008, 12:00am UTC](https://www.ruby-forum.com/t/rjs-happening-too-fast/130404 "2008-02-28T00:00:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 28, 2008, 12:00am UTC](https://www.ruby-forum.com/t/rjs-happening-too-fast/130404/1 "2008-02-28T00:00:09Z")

</div>

I’m working on adding tagging to my application, to add tags to a  
mailing list of fans.

I’ve got checkboxes to select each fan, and a dropdown select list  
with the current tags and a “New tag” option. If you choose “New  
tag”, a Javascript prompt asks you to name the new tag, and then it  
calls the method which adds the tags to the list of fans.

So far so good, but the last thing I want it to do is refresh the list  
of fans with the new tags applied. Right now it’s refreshing the list  
of fans, except it’s the previous list of fans without the new tags.  
If I manually reload the page, the tags are clearly there, but my  
guess is the inline RJS is happening too fast, so that it’s refreshing  
the fans\_list DIV before the new tags have been applied? I’m not  
sure.

Here’s the function called from my form\_observer:

FansController#new\_tag

def new\_tag  
if params[:tag] == “New tag”  
render :update do |page|  
session[:fans] = params[:fans]  
unless params[:fans].empty?  
page.call(“x\_add\_tag”)  
@fans = Fan.paginate :all, :per\_page =\> 25, :page =\>  
params[:p], :order =\> “created\_at DESC”  
page.replace\_html ‘fans\_list’, :partial =\>  
‘fans\_list’, :locals =\> {:fans =\> @fans, :what =\> “FUNK YEAH”}  
end  
end  
else  
render nil, :layout =\> false  
end  
end

What am I doing wrong? Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 28, 2008, 12:32am UTC](https://www.ruby-forum.com/t/rjs-happening-too-fast/130404/2 "2008-02-28T00:32:57Z")

</div>

Jeff C.man wrote:

> ```
> page.call("x_add_tag")
> 
> ```

Are you creating the new fan here? If so, isn’t this going to get  
executed in the browser? So you are sending back javascript to:

1. add the tag
2. update the select list

Doesn’t this mean the new tag is only added after another trip to the  
browser but the new fan list is evaluated in the method? Or have I  
interpreted this all wrong?

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 28, 2008, 12:33am UTC](https://www.ruby-forum.com/t/rjs-happening-too-fast/130404/3 "2008-02-28T00:33:26Z")

</div>

Mark B. wrote:

> Are you creating the new fan here? If so, isn’t this going to get

Creating the new tag I mean…

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 28, 2008, 12:47am UTC](https://www.ruby-forum.com/t/rjs-happening-too-fast/130404/4 "2008-02-28T00:47:46Z")

</div>

On Feb 27, 5:32 pm, Mark B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Jeff C.man wrote:
> 
> > ```
> > page.call("x_add_tag")
> > 
> > ```
> 
> Are you creating the new fan here?

Here’s the JavaScript, which is in my view:

\<%= javascript\_tag %Q{  
function x\_add\_tag()  
{  
var tag = prompt(‘Name for new tag’, ‘’);  
if (tag == null) return;  
#{remote\_function :url =\> tag\_fans\_path(:fans =\>  
@fans\_to\_tag), :with =\> “‘tag=’ + tag” }  
}

} %\>

And here’s the “tag” action in fans\_controller.rb:

def tag  
fans = params[:fans]  
fans ||= session[:fans]  
for f in fans  
fan = Fan.find(f[1])  
tag ||= params[:tag]  
tag = params[:fan][:tag] if params[:fan]  
fan.tag\_list.add(tag)  
fan.save  
end  
redirect\_to fans\_path  
end

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 28, 2008, 12:57am UTC](https://www.ruby-forum.com/t/rjs-happening-too-fast/130404/5 "2008-02-28T00:57:47Z")

</div>

So I believe what is happening is:

1. user invokes #new\_tag remote function
2. #new\_tag renders some javascript which includes:  
a. something to popup asking for the new tag, then remote calling #tag  
b. something to replace the fans list (after getting the _current_  
list)
3. the javascript is evaluated in the browser:  
a. displaying the popup and calling the remote function  
b. setting the selection list from @fans as it was when #new\_tag ran
4. #tag adds the new tag

I think you need to have your replace of the selection list in the  
render of #tag so that @fans is evaluated after the new tag is created.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 28, 2008, 6:40am UTC](https://www.ruby-forum.com/t/rjs-happening-too-fast/130404/6 "2008-02-28T06:40:28Z")

</div>

On Feb 27, 5:57 pm, Mark B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I think you need to have your replace of the selection list in the  
> render of #tag so that @fans is evaluated after the new tag is created.

Okay, that sounds right! I’ll work on that.
