對抗垃圾信!請您點這裡:

我的E-mail:
我的Skype:My status

2007年3月4日

Rails -- InPlaceEdit

用過Flickr嗎?
如果你有Flickr相簿,應該對於修改照片標題、說明的方式記憶猶新吧?
那種就叫做 In Place Editing
在Rails中,要實做這種技術並不難,因為本身就內建這個功能
不過到了Rails 2.0將會把這個從內建移除變成Plugins形勢存在
可以參考這篇:In-plcae-editing by Rails
不過我在這裡重新說明一次使用方式吧
如果有<%= javascript_include_tag :defaults %>的話,那只剩下兩個步驟:
Controller:

class ObjectController < ApplicationController
in_place_edit_for :object, :method
end


View:

<%= in_place_editor_field :object, :method %>

這樣就可以建立起最基本的InPlaceEditing欄位
可是最基本的都是英文,因此Rails也提供了修改參數,可以參考這篇
in_place_editor_field欄位有四個參數:
in_place_editor_field(object, method, tag_options = {}, in_place_editor_options = {})
而修改的部分則是放在第四個參數;假設我要修改:saving_text:
<%= in_place_editor_field(:object, :method, {}, {:saving_text => "儲存中..."} %>
改好後記得重新整理頁面!

另外,如果要建立多個欄位的話,必須用這種方法:
class ObjectController < ApplicationController
in_place_edit_for :object, :method1
in_place_edit_for :object, :method2
in_place_edit_for :object, :method3
end


這樣寫超麻煩的!因此可以這樣:

class ObjectController < ApplicationController
%w"method1 method2 method3".each do |m|
in_place_edit_for :object, m.to_sym
end
end

這樣未來在新增刪除上都會很方便!

沒有留言: