Ajax and ruby problem highlighting


#1

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    
<%= 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


#2

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


#3

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


#4

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?