Rails4ã§ç°¡åAjax
Railsã¢ããªã±ã¼ã·ã§ã³ã§Ajaxãå®è£ ããéã«ããªãèºããã®ã§æé ã®ã¡ã¢ã
å··ã§ã¯ãformã§remote: trueãããã ãã®ç°¡åãªãä»äºã ã¨è¨ããã¦ãããã©ãæµããåãã£ã¦ããªãã¨åå¤é£ããããã«æãã
å®éåé¡ã大ä½ã®ä»çµã¿ãããããã°ãjQueryã§ajaxè¦æ±åºãå¿
è¦ããªãããã¨ã¦ã簡便ã«ajaxãå®è£
åºæ¥ãããã«ãªã£ã¦ããã¨æãã
ã¨ããããã§æ©é復ç¿ã
ãµã³ãã«ã®å¶ä½
ä»åã¯ã¨ããããã¡ãã»ã¼ã¸ã®æ稿ã ããè¡ãç°¡æã¢ããªãä½ããAjaxåããã
scaffoldã¯ä¾¿å©ã ãã©ããã®ç¨åº¦ã®è¦æ¨¡ã®å ´åã¯ä½¿ããªãã»ããæ解ã ã¨æãã®ã§ãä¸ã¤ä¸ã¤æä½ããã¦ããã
ä½ã¯ã¨ãããæ°è¦ããã¸ã§ã¯ããä½æããã¢ãã«ããã³ã³ã³ããã¼ã©ã®ä½æã
rails new Bwitter
cd Bwitter
rails g model bweet description:text
rails g controller bweets index create
rake db:create
rake db:migrate
rails s
ãã¤ãã¢ãã«ã§æå®ããåã¨ããå¿ãã¦ãã¾ãããã¤ã«ãªã£ããè¦ããããã®ãâ¦â¦ã
ã¨ããããã¢ãã«ã¯ã¡ãã»ã¼ã¸æ ¼ç´ã®ããã®textådescriptionã¨ããã«ã©ã ãä½æããã³ã³ããã¼ã©ã¼ã¯æ稿ãããã¡ãã»ã¼ã¸ä¸è¦§ã表示ããããã®indexã¢ã¯ã·ã§ã³ã¨ãã¡ãã»ã¼ã¸ã®DBæ ¼ç´ç¨ã«createã¢ã¯ã·ã§ã³ã®äºã¤ãå¶ä½ãå¤åæå°æ§æã ã¨æãã
ãµã¼ãã¼èµ·åå¾localhost:3000ã«ã¢ã¯ã»ã¹ããã¨è¦ªãã¿æ·±ãç»é¢ãã
ã¾ãã¯ã«ã¼ãã£ã³ã°ããã
æããåæç¶æ
ã ã¨getã¡ã½ããã®indexã¨createãããã¨æãããã®ã§ã
#config/routes.rb root :to => 'bweets#index' get "bweets/index" post "bweets/create"
ã¨ããæãã§ãrootã¢ã¯ã·ã§ã³ãindexã«å¤æ´ããã³createã®ã¡ã½ãããpostã«å¤æ´ã
次ã«indexã¢ã¯ã·ã§ã³ã®ç·¨éãã³ã³ããã¼ã©ããã
#app/controllers/bweets_controller.rb def index @bweets = Bweet.order("created_at DESC").limit(10) end
ã¨ãããããæ°ããæ稿ã10件åå¾ããæãã§ã
Railsã§Ajaxãç°¡åã«å®è£
ããããã«ã¯ãAjaxã§è¡¨ç¤ºãããè¦ç´ ããé¨åãã³ãã¬ã¼ãã§ä½æããã®ãã³ãã£ã½ãã
indexã§ã¯ãæ稿ãããã¡ãã»ã¼ã¸ã®ä¸è¦§è¡¨ç¤ºãAjaxåãããããããããé¨åãã³ãã¬ã¼ãã§ä½æããã
ã¨ããããã§ãapp/views/bweets以ä¸ã«ã_bweet.html.erbã¨ããé¨åãã³ãã¬ã¼ããã¡ã¤ã«ãæ°è¦ä½æããã
#app/views/bweets/_bweet.html.erb <% @bweets.each do |bweet| %> <fieldset> <legend><%= bweet.created_at %></legend> <p><%= bweet.description %></p> </fieldset> <% end %>
表示ãææãããã«fieldsetã使ã£ã¦ãããã©ã大ããæå³ã¯ç¡ãã
å®ã¯é¨åãã³ãã¬ã¼ãã®ä¸ã§ã¯ãrenderæã«æã£ã¦ããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã@çç¥ã®ãã¡ã¤ã«åã§å©ç¨ã§ããããã³ã¬ã¯ã·ã§ã³ã®ã«ã¼ããããããã¨ãè²ã è¤éã£ã½ããã¨ããããã¯ããã§ãç解ãæ·±ããã追ã 使ãããªãã¦ããããã
é¨åãã³ãã¬ã¼ãã¯åºæ¥ãã®ã§ãindex.html.erbã®ä¸ã«åãè¾¼ãã§ãããã°OKã
ãã£ãããªã®ã§æ稿ãã©ã¼ã ãä¸ç·ã«ä½æã
æ稿ãã©ã¼ã ã¯Twitter風ã«ãindexç»é¢ã®æ¨ªå´ã«è¡¨ç¤ºã
<div id='wrap'> <div id='sideWrap'> <%= form_tag( { action: :create }, { remote: true } ) do %> ãã<fieldset> ãã<legend>Bweet</legend> ãã<div><%= text_area_tag :description%></div> ãã<div><%= submit_tag :Bweet %></div> ãã</fieldset> ãã<% end %> </div><!-- /sideWrap --> <div id='mainWrap'> <h1>BweetsList</h1> <div id='bweets'> <%= render partial: "bweets", collections: @bweets %> </div> </div><!-- /mainWrap --> </div><!-- /wrap -->
divã¯å¾ã§å¤å°ä½è£ãæ´ããããã ããªã®ã§ãåºæ¬çã«ã¯æ°ã«ããå¿
è¦ã¯ãªãã
ãã ãé¨åãã³ãã¬ã¼ãã®å¼ã³åºããè¡ã£ã¦ããé¨åã®divã®idã¯ãAjaxã§ã®è¦ç´ å
¥ãæ¿ãã§å©ç¨ããã®ã§ãè¨å®ã®å¿
è¦ãããã
é¨åãã³ãã¬ã¼ãã®å¼ã³åºãã¯ãrenderã®partialãªãã·ã§ã³ã§æå®ãããåããå¤æ°ã¯collectionsã§æ¸¡ãã
form_tagã§ã¯ãªãã·ã§ã³ã¨ãã¦ãremote: trueãè¨å®ããã
å®éåé¡formã®Ajaxåã¯ãã®ãªãã·ã§ã³ã ãã§å®äºã
ã¡ãªã¿ã«form_tagã®ãªãã·ã§ã³ã¯ã1ã¤ç®ã®ããã·ã¥ãã¢ã¯ã·ã§ã³ã®æå®ã2ã¤ç®ã®ããã·ã¥ããã®ä»ã®ãªãã·ã§ã³ã®æå®ã¨ããæãã ã£ãã¨æããçµæ§ããã§ãã¨ã©ã¼ãåãããã
表示ã®é¨åã¯å¤§å¡åºæ¥ãã®ã§ã次ã¯createã®æ¹ãä½ãã
createãã§ããªãã¨è¡¨ç¤ºã®ç¢ºèªããåºæ¥ãªãï¼DBç´æ¥å©ãã°å¯è½ã§ã¯ãããâ¦â¦ï¼ã
#app/controller/bweets_controller.rb def create @bweets = Bweet.order("created_at DESC").limit(10) @bweet = Bweet.new(bweet_params) respond_to do |format| if @bweet.save format.js else render "index" end end end private def bweet_params params.permit(:description) end
createã§ã¯ãéä¿¡ããããã©ã¡ã¼ã¿ãåå¾ããbweetã®æ°è¦ä½æã¨ä¿åã¨ãããä¸è¬çãªæ ¼ç´ã®æé ããã®ã¾ã¾è¸ãã§ããã
ä¸ã¤ç¹æ®ãªã®ããrespond_toã®formatã«format.jsãæå®ãã¦ãããã¨ã
ãããç¨æãããã¨ã§ãå¾è¿°ããcreate.js.erbã®å¼ã³åºããè¡ãããã
privateã§å®ç¾©ãã¦ããbweet_paramsã¡ã½ããã¯ãRails4ã§å°å ¥ãããstrong_parametersã®ããã«å®ç¾©ãã¦ããã ããããã§ã¯è©³ããè¿°ã¹ãªãã
æåã®bweet10件åå¾ã¯ãå¾ã®æåæã«@bweetsã®å¤ãå¿ è¦ãªããããã£ã¨ã¹ãã¼ããªããæ¹ã¯ãããã§ãããããã
ã¨ããããç¾ç¶ã§formã«å¤ãå
¥ãsubmitããã¨ãç¹ã«è¡¨ç¤ºã®å¤åãèµ·ããªãäºã確èªåºæ¥ãã¨æãã
æ´æ°ãæ¼ãã¨ãæ稿ããã¡ãã»ã¼ã¸ãç¾ããã¯ãã
â»cssã§floatã®è¨å®ã ãè¡ã£ã
å
ã»ã©createå
ã®formatã§jsãæå®ãããã¨ã§ãcreateã¢ã¯ã·ã§ã³æã«create.js.erbãå¼ã°ããããã«ãªã£ãã
app/views/bweets以ä¸ã«ãcreate.js.erbãæ°è¦ä½æããAjaxã§å¦çãããããbweetã®è¡¨ç¤ºé¨åãã³ãã¬ã¼ãã®å¼ã³åºããè¨è¿°ããã
$('#bweets').html("<%=j render partial: 'bweet', collections: @bweet %>"); $('#description').val("");
ãããªæãã
jã¯javascriptã§äºã
ããã¬ããªã¡ã½ãããããã
ç»åã§ã¯è¡¨ç¾åºæ¥ãªããã©ãç¡äºæ稿ã¨åæã«ãBweetsListã«æ°è¦æ稿ã表示ãããã®ã§ã¯ãªããã¨æãã
ã¾ã¨ã
復ç¿ãã¦ãã«ç°¡åãªAjaxã¢ããªãä½ã£ã¦ã¿ããã©ãå度ã®ä½æ¥ã§ãè¥å¹²ã®èºãããã£ãã
Railsèªä½ã«ãã¾ãæ
£ãã¦ããªãã®ã¨ä½µãã¦ãçµæ§è¾ãã
Rails4ããã¯turbolinksãªããã®ãæ¨æºæè¼ããã¦ããã®ã§ãjQueryã使ãå ´åã¯ç«¶åã«æ°ã使ã£ããããå¿
è¦ãããæ§åãä»åã®Ajaxåææ³ã®å ´åã¯ãç¹æ®µturbolinksã«é¢ããè¨å®ã¯å¼ããå®è£
ã§ããã
äºæ³å¤ã«è¨äºã¯é·ããªãããè¦æ¨ãçºã¾ã£ã¦ãªããã§ææªã§ã¯ãããã©ãã¨ããããRailsãAjaxãããããï¼ã£ã¦ãã¨ã§èª¤éåãã
ã»ä½è«
Railsã®Ajaxã¹ã¿ã¤ã«ã¯ãæãããã®å½¢ãæãç°¡åãªã®ã§ã¯ãªããã¨æãããä¸å¿jQueryã®ajaxã¡ã½ããã使ã£ãAjaxåããå¾ã
æé ãã¡ã¢ãã¤ããâ¦â¦ãã¤ã®æ¥ãâ¦â¦ã
åè
Railsã«ããã¢ã¸ã£ã¤ã«Webã¢ããªã±ã¼ã·ã§ã³éçº ç¬¬4ç
ããã¼ã¹ã«ä½æãã¾ããã
Ruby on Rails 3 の Ajax いろいろ - @yuumi3のお仕事日記
form_tag - リファレンス - Railsドキュメント
ãã®ä»è²ã
Webãµã¤ã
ï¼æ¸ãè¨ããªããããè¨å¤§ãªæ°ã®ãã¼ã¸ãåèã«ãã¾ãããï¼