1, Client
<%=link_to_remote 'Check Availability',
:submit => "signupForm",
:url => {:controller => 'users', :action => 'check_username_avaiable' },
:update => 'check_username',
:html => {:class => 'smallText'} %>
:submit => "signupForm",
:url => {:controller => 'users', :action => 'check_username_avaiable' },
:update => 'check_username',
:html => {:class => 'smallText'} %>
注意这里的submit 是Form的ID, update是ajax回来以后要update的DOM的ID
2, server
def check_username_avaiable
exist_user = User.find_by_username(params[:username])
exist_user = 'yes' if params[:username] == 'demo'
if exist_user.nil? then
render :partial=>'username_avaiable', :locals => {:username => params[:username]}
else
render :partial=>'username_not_avaiable', :locals => {:username => params[:username]}
end
end
exist_user = User.find_by_username(params[:username])
exist_user = 'yes' if params[:username] == 'demo'
if exist_user.nil? then
render :partial=>'username_avaiable', :locals => {:username => params[:username]}
else
render :partial=>'username_not_avaiable', :locals => {:username => params[:username]}
end
end