上一遍我介紹了observe_field,我們知道這些方法都是rails封裝好的ajax實現,例如還有下面的這些方法,如果想了解更多,請查閱api PrototypeHelper
build_callbacks
build_observer
button_to_remote
evaluate_remote_response
form_remote_for
form_remote_tag
link_to_remote
method_option_to_s
observe_field
observe_form
options_for_ajax
periodically_call_remote
remote_form_for
remote_function
submit_to_remote
update_page
update_page_tag
這次用form_remote_tag 主要是為了驗證用戶登陸的,為了改善用戶的人性化使用,所以能用ajax實現的最好用ajax實現
實現效果:
實現過程:
rhtml:
<span id="loginerror" class="error"></span>
<%= form_remote_tag(:url => { :action => :login }) %>
……………………….
<%= form_tag %>
|
controller:
if 驗證成功 render :update do |page| page.redirect_to :controller=>'myorganizer',:action=>'index' end else render :update do |page| page.replace_html 'loginerror', '用戶名或者密碼錯誤' end |
注意:
1,顯示信息的id最好在form的上面,不要在form的里面,不然可能會出錯
2,使用這種 render :update的方式,不要使用,form_remote_tag 的:update選項的方式(使用這種方式,錯誤可以提示,但是正確了不可以跳轉,因為你更新的是那個指定的id),不然的話會出現下面的錯誤:rjs錯誤
ref:
http://www.javaeye.com/topic/34655
http://railsforum.com/viewtopic.php?id=8510
http://jmvidal.cse.sc.edu/talks/rubyonrails/form_remote_tag.html
posted on 2009-04-29 18:21
fl1429 閱讀(2368)
評論(2) 編輯 收藏 所屬分類:
Rails