Rails3ããã®å¤æ´ç¹ã§ããã£ãã®ã§ã¡ã¢ã
Rails2ã§ã¯formãAjaxã§éä¿¡ããã«ã¯form_remote_tagãå©ç¨ãã¦ããã
ãããRails3ã§ã¯ä»¥ä¸ã®ããã«:remote => trueãæå®ãããã¨ã§å®ç¾ããã
<%= form_tag(url_for(:action => 'create'), :remote => true, :id => "result_form") do %> <% end %>
ä»ã¯ä¸»ã«jQueryã使ã£ã¦éçºãã¦ããçºãããã©ã«ãã§æ±ãjavascriptãjQueryã«å¤æ´ããã
Gemfileã«ä»¥ä¸è¨è¿°
gem 'jquery-rails'
bundle installãã¦é¢é£ãã¡ã¤ã«ãgenerate
$ bundle install $ rails g jquery:install
jquery.jsã¨rails.jsãèªã¿è¾¼ãããã«è¨å®
jquery.jsã¯googleããèªã¿è¾¼ã¿
#app/views/layouts/application.html.erbçã«è¨è¿° <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <%= javascript_include_tag 'rails' %>
formããã®ãªã¯ã¨ã¹ããåãåãcallbackãJavascriptãã¡ã¤ã«ã«è¨å®
$(function($){ $('#result_form') .bind("ajax:complete", function(){ //ããã«å ·ä½çãªå¦çãè¨è¿° }); });
ä»ã®ã¹ãã¼ã¿ã¹ã«ã¤ãã¦ãè¨è¿°å¯è½
ããããå¼æ°ãåãåããã¨ãã§ããã
$(function($){ $('#result_form') .bind("ajax:loading", function(xhr){ //ããã«å ·ä½çãªå¦çãè¨è¿° }) .bind("ajax:success", function(data, status, xhr){ //ããã«å ·ä½çãªå¦çãè¨è¿° }) .bind("ajax:complete", function(xhr){ //ããã«å ·ä½çãªå¦çãè¨è¿° }) .bind("ajax:failure", function(data, status, xhr){ //ããã«å ·ä½çãªå¦çãè¨è¿° }); });
å¦çãç¡åé¢æ°ã«è¨è¿°ã§ããã®ã§Rails2ã§ã®ããã«viewã«ã´ãã£ã£ã¨æ¸ãå¿ è¦ããªããªããããæ確ã«ãªã£ãã¨æãã