# Ajax and ruby problem highlighting

**URL:** <https://www.ruby-forum.com/t/ajax-and-ruby-problem-highlighting/48494>\
**Category:** Rails\
**Created:** [November 21, 2005, 2:14pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-highlighting/48494 "2005-11-21T14:14:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 2:14pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-highlighting/48494/1 "2005-11-21T14:14:59Z")

</div>

hey, i starting to use ajax in my site and i hope that u guys can help  
me,

for an simple example, i have a list of users

Users  
Lastname Firstname  
Brutyna Andydsfd Show Edit  
Brutynd Lucs Show Edit  
Brutynz Nick Show Edit

this is the list, when u click on show, u get the ajax “popup” with the  
info.  
when u click on edit, u get also an ajax “popup”, there u can edit the  
user  
values. when i click on the the update button (in the edit popup), i  
want this  
to happen:  
-popup closes  
-refresh user list  
-highlight the edited user  
==\> all this works with a static id like “user5” (this is the row in the  
list)  
but i want a dynamic id  
(see in edit.rhtml)  
:complete =\> ‘user\_updated(“user5”);’ ) %\>)

## this is my code for the index.rhtml

\<% content\_for(“page\_scripts”) do -%\>

function changeSty(id, newClass){  
//var item = $(‘items’).firstChild;  
//new Effect.Highlight(item);  
item=document.getElementById(id);  
item.className=newClass;

}

function user\_updated(item) {  
$(‘form-submit-button’).disabled = false;  
Element.hide(‘busy’);  
//popup becomes hidden  
changeSty(“div\_user”,“noshow\_link”);  
changeSty(“info”,“noshow\_link”);  
//mark the edited row  
new Effect.Highlight(item);  
}

function item\_loading() {  
$(‘form-submit-button’).disabled = true;  
Element.show(‘busy’);  
}  
\<% end -%\>

\<% if @users != nil %\> \<% if !@users.empty? %\> \<% for user in @users %\> \<% end %\> \<% else %\> \<% end %\> \<% end %\>

| Users |
| --- |
| Lastname | Firstname | &nbsp; | &nbsp; |
| --- | --- | --- | --- |
| \<%= user.last\_name %\> | \<%= user.first\_name %\> | \<%= link\_to\_remote ("Show", :update =\> "div\_user", :url =\> { :action =\> :show, :id =\> user }, :complete =\> 'changeSty("div\_user","show\_popup")' ) %\> | \<%= link\_to\_remote ("Edit", :update =\> "div\_user", :url =\> { :action =\> :edit, :id =\> user }, :complete =\> 'changeSty("div\_user","show\_popup")' ) %\> |
| No entries found... |

* * *

## my edit.rhtml

| User Info | Close |
| \<%= form\_remote\_tag ( :url =\> { :action =\> :update, :id =\> @user }, :update =\> "userlist" , :loading =\> 'item\_loading()', :complete =\> 'user\_updated("user5");' ) %\> 

| 
## User details

**Lastname:** \<%= text\_field "user", "last\_name", :size =\> 20, :id =\> 'user[last\_name]' %\>

**Firstname:** \<%= text\_field "user", "first\_name", :size =\> 20, :id =\> 'user[first\_name]' %\>

**Email:** \<%= text\_field "user", "email", :size =\> 20, :id =\> 'user[email]' %\>

 |
| \<%= submit\_tag("Update", :id =\> 'form-submit-button') %\> Updating... |

 |

* * *

## my controller

class UserController \< ApplicationController

```
def index
	@users = User.find(:all, :order => "last_name ASC, first_name ASC")
	updated = params[:updated].blank? ? false : params[:updated]
	if updated
		render(:layout => false )
	end
	rescue
end

def show
	user_id = params[:id]
	@user = User.find(:first, :conditions =>['id = ? ', user_id])
	render(:layout => false )
end

def edit
	user_id = params[:id]
	@user = User.find(:first, :conditions =>['id = ? ', user_id])
	render(:layout => false )
end

def update
	@user = User.find(params[:id])

	@user.last_name = @params[:user][:last_name]
	@user.first_name = @params[:user][:first_name]
	@user.email = @params[:user][:email]
	if @user.save
		redirect_to :controller =>'user', :action => 'index', :updated => 

```

true #dont  
think this is a right way to do it, when the update is done, i dont need  
to  
render the global layout again  
else  
redirect\_to :controller =\>‘user’, :action =\> ‘edit’, :id =\> @user  
end

```
end

```

end

* * *

when i change (in edit.rhtml) the complete stuff:

:complete =\> ‘user\_updated(“user5”);’ ) %\>

to all the javascript function like  
:complete =\> ‘user\_updated(“user5”);changeSty(“info”,“noshow\_link”);new  
Effect.Highlight(“user5”)’  
he doesnt want to do all this, and something the layout is fucked up

hope u guys can help me

thanks alot

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 2:47pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-highlighting/48494/2 "2005-11-21T14:47:23Z")

</div>

It is difficult for me to follow all the code, so I am suggesting you to  
use  
Firefox and watch the javascript console, looking for javascript errors.  
What is user5?  
On edit.rhtml you should probably have:  
\<%= form\_remote\_tag ( :url =\> { :action =\> :update, :id =\> @user.id },  
:update =\> “userlist” ,  
:loading =\> ‘item\_loading()’,  
:complete =\> “user\_updated(‘user#{@user.id}’);” ) %\>

bogdan

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 3:03pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-highlighting/48494/3 "2005-11-21T15:03:28Z")

</div>

user5 is just to make a unique id , text “user” and its id, "cuz i  
thougth ids  
couldnt start with numbers

yeah thanks for that  
changed it to  
\<% …

:complete =\>  
“new EFfect.Highlight(‘user#{@user.id}’);  
user\_updated();changeSty(‘info’,‘noshow\_link’);” ) %\>

And how can i now make the popup draggable???

\<%= draggable\_element(“div\_user”, :revert =\> true) %\> doesnt seem to  
work

div =\> div\_user is the div that is already on the index page (invisible,  
and  
position absolute)

div =\> info is the div with the table and its user info (popup)

thanks in advance

---

<div class="post-metadata">

**Author:** ![brutyn\_nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@brutyn\_nick](https://www.ruby-forum.com/u/brutyn_nick)\
**Post date:** [November 21, 2005, 3:59pm UTC](https://www.ruby-forum.com/t/ajax-and-ruby-problem-highlighting/48494/4 "2005-11-21T15:59:54Z")

</div>

i have fixed the previous error, no thats works, but now i want to  
create a new  
user and highlight it after creation

with my previous code in edit.html, i now have in new.rhtml

edit.rhtml  
\<%= form\_remote\_tag ( :url =\> { :action =\> :update, :id =\> @user },  
:update =\> “userlist” ,  
:loading =\> ‘item\_loading()’,  
:complete =\> “new Effect.Highlight(‘user#{@user.id}’);  
user\_updated();changeSty(‘info’,‘noshow\_link’);”  
) %\>

new.rhtml  
\<%= form\_remote\_tag ( :url =\> { :action =\> :create, :id =\> @user },  
:update =\> “userlist” ,  
:loading =\> ‘item\_loading()’,  
:complete =\> “user\_updated();changeSty(‘info’,‘noshow\_link’);” ) %\>

here i also need new Effect.Highlight(‘user#{@user.id}’) , but i dont  
now the  
user id yet, cuz i need to created if first, can anyone help here?
