close

Rails 中使用 check box 來作 ajax 的控制,其中關鍵的指令是 remote_function。用它來作後端呼叫。

這是 html 的 check box

<input type="checkbox"  'checked' />

rails 的 check box 指令如下:

<%= check_box(answer.dom_id, "picking",
{ :checked => answer.checked,
:onclick => remote_function(:url => {:controller => "picking", :action => "update", :answer_id => answer.id})}) %>


呼叫完,可 render :action 一個 rjs 檔來反應網頁的變化。比如 highlight 變動的物件。

page.visual_effect :highlight, @answer.dom_id, :duration => 2




花了很久的時間,試不出來在點擊後,將更新的狀態存入資料庫裡。
透過表單? 抓不到值。透過 prototype 的 $ 函數,不成功。
最後,暫時用值的正負切換來作。反正 user 點擊後,新的資料一定是原來資料的負值。直接在 controller 裡改變就好了。至於頁面中的 checkbox,將態一定是跟資料庫一致的。因為都是一正一負的切換。

arrow
arrow
    全站熱搜

    cactis 發表在 痞客邦 留言(0) 人氣()