用RJS写的检测用户名和email是否存在
1, Client的check url
<%=link_to_remote 'Check Availability',
:submit => "signupForm",
:url => {:controller => 'users', :action => 'check_avaiable' },
:html => {:class => 'smallText'} %>
:submit => "signupForm",
:url => {:controller => 'users', :action => 'check_avaiable' },
:html => {:class => 'smallText'} %>
2, Controller的action
def check_avaiable
respond_to do |format|
format.html { redirect_to '/' }
format.js
end
end
respond_to do |format|
format.html { redirect_to '/' }
format.js
end
end
3, RJS 文件
代码
exist_user = User.find_by_username(params[:username])
if exist_user.nil? then
page.replace_html :check_username, :partial => 'check_avaiable', :locals=>{:check_value => params[:username]}
else
page.replace_html :check_username, :partial => 'check_not_avaiable', :locals=>{:check_value => params[:username]}
end
exist_email = User.find_by_email(params[:email])
if exist_email.nil? then
page.replace_html :check_email, :partial => 'check_avaiable', :locals=>{:check_value => params[:email]}
else
page.replace_html :check_email, :partial => 'check_not_avaiable', :locals=>{:check_value => params[:email]}
end
if exist_user.nil? then
page.replace_html :check_username, :partial => 'check_avaiable', :locals=>{:check_value => params[:username]}
else
page.replace_html :check_username, :partial => 'check_not_avaiable', :locals=>{:check_value => params[:username]}
end
exist_email = User.find_by_email(params[:email])
if exist_email.nil? then
page.replace_html :check_email, :partial => 'check_avaiable', :locals=>{:check_value => params[:email]}
else
page.replace_html :check_email, :partial => 'check_not_avaiable', :locals=>{:check_value => params[:email]}
end