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 -%>
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");' ) %>
|
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