ミームの死骸を待ちながら

We are built as gene machines and cultured as meme machines, but we have the power to turn against our creators. We, alone on earth, can rebel against the tyranny of the selfish replicators. - Richard Dawkins "Selfish Gene"

We are built as gene machines and cultured as meme machines, but we have the power to turn against our creators.
We, alone on earth, can rebel against the tyranny of the selfish replicators.
- Richard Dawkins "Selfish Gene"

AjaxなRails

Railsで使い易いのは

link_to_remote()

<%= link_to_remote( "click here",
:update => "目的のdiv",
:url => { :action => :controllerのmethod }) %>

methodは例えば

render_text "hogehoge" + Rubyな処理

んでメソッド実行された後に対応するviewが書かれるんだっけ。中で起こってることは、clickするとXMLHttpRequestを生成しアクションハンドラを実行するURLに送る。でHTMLの断片が帰ってきて、JavaSctiptはそれを受け取って指定されたdiv idに挿入。link_to_remoteのオプションで

:position => "after"

とかやれば置換しない。afterとbottomの違いは、挿入する位置がエレメントの外側か内側か。

form_remote_tag()

View内に以下のように書く

<%= form_remote_tag(:update => "my_list",
:url => {:action => :add},
:position => top) %>

New item text:
<%= text_field_tag :newitem %>
<%= submit_tag "Add item" %>
<%= end_form_tag %>

<ul id="mylist">
<li>hogehogehoge
</ul>

こうやれば次々リスト化。ただし、Controller内のaddってメソッドは

def add
render_text "<li>" + params[:newitem] + "</li>"

と、一つ一つに<li>タグ付けねばならん。paramsはformからsubmitされてきたパラメタ。

フィールドの監視。

<%= text_field_tag :searchtext %>
<%= observe_field(:searchtext,
:frequency => 0.25,
:update => :search_hits,
:url => {:action => :live_search })
%> 

frequencyは何秒ごとにチェックするか。変化してた場合はlive_searchアクションハンドラを実行、search_hitsってdivをupdateする。if文とかいらんのがイイ。

以上、ONLamp.com -- Ajax on Railsとその和訳を参考に。

そういえば

LightboxJS普通に←→できた。