Rails勉強会@大阪 第0回 - Ajax with Rails - 資料 [sample code 1] mini-Ajax * hello.txt: Hello, Ajax! * list1.html:
[sample code 2] mini-Ajax with prototype.js * list3.html:
[sample code 3] mini-Ajax with Rails $ rails demo $ cd demo $ ./script/generate controller example * app/views/layouts/example.rhtml: <%= javascript_include_tag "prototype" %> <%= @content_for_layout %> * app/views/example/sample1.rhtml: <%= link_to_remote "Hello?", :url => {:action => :hello_ajax}, :update => :hello %>
* app/controllers/example_controller.rb: require 'kconv' class ExampleController < ApplicationController def hello_ajax now = Time.now render :text => "Hello, Ajax! (#{now})".toutf8, :layout => false end end [sample code 4] Incremental search * app/views/example/observer.rhtml: Search term: <%= text_field_tag :search %> <%= observe_field :search, :frequency => 0.5, :update => :results, :url => {:action => :search}, :with => "'phrase=' + encodeURIComponent(value)" %>
* app/controllers/example_controller.rb: # in class ExampleController... WORDLIST = %w(Rails is a full-stack, open-source web framework in Ruby for writing real-world applications with joy and less code than most frameworks spend doing XML sit-ups) def search @phrase = params[:phrase] matcher = Regexp.new(@phrase) @results = WORDLIST.select {|word| matcher =~ word } render :layout => false end * app/views/example/search.rhtml: <% if @results.empty? %> '<%=h @phrase %>' not found! <% else %> '<%=h @phrase %>' found in <%= highlight(@results.join(", "), @phrase) %> <% end %> [参考文献] * 『開発の現場 vol.2』【特別記事】Ruby on RailsでらくらくAjax開発!/舘野祐一 サンプルデータ: http://www.shoeisha.com/mag/kaihatsu/sample/kaihatsu002_RoR.lzh * AWDwR BETA-BOOK p.375 18.2 The Rails Way | Observers