in_place_editor用法

秦才良
2023-12-01

今天想做一个在线编辑的东西,翻遍了图书,讲的都是用in_place_edit_for在controller里面添加一个方法,然后在相应的视图里面添加一个in_place_editor_field的help方法,在实现现场编辑功能,但是如果需要实现现场编辑功能的时候,并没有配套的controller怎么办……查询了Rails的类库,发现除了上述提到的2个以外,还有一个in_place_editor方法,直接在视图里面添加,不需要另外添加什么方法,直接指定需要现场编辑的HTML元素的DOM ID即可。

 

以下摘自Rails1.2.6的类库:

in_place_editor(field_id, options = {})

DEPRECATION WARNING: This method will become a separate plugin when Rails 2.0 ships.

Makes an HTML element specified by the DOM ID field_id become an in-place editor of a property.

A form is automatically created and displayed when the user clicks the element, something like this:

<form id="myElement-in-place-edit-form" target="specified url">
    <input name="value" text="The content of myElement"/>
    <input type="submit" value="ok"/>
    <a οnclick="javascript to cancel the editing">cancel</a>
  </form>

The form is serialized and sent to the server using an AJAX call, the action on the server should process the value and return the updated value in the body of the reponse. The element will automatically be updated with the changed value (as returned from the server).

Required options are:

:url:Specifies the url where the updated value should be sent after the user presses "ok".

Addtional options are:

:rows:Number of rows (more than 1 will use a TEXTAREA)
:cols:Number of characters the text input should span (works for both INPUT and TEXTAREA)
:size:Synonym for :cols when using a single line text input.
:cancel_text:The text on the cancel link. (default: "cancel")
:save_text:The text on the save link. (default: "ok")
:loading_text:The text to display while the data is being loaded from the server (default: "Loading…")
:saving_text:The text to display when submitting to the server (default: "Saving…")
:external_control:The id of an external control used to enter edit mode.
:load_text_url:URL where initial value of editor (content) is retrieved.
:options:Pass through options to the AJAX call (see prototype’s Ajax.Updater)
:with:JavaScript snippet that should return what is to be sent in the AJAX call, form is an implicit parameter
:script:Instructs the in-place editor to evaluate the remote JavaScript response (default: false)

:click_to_edit_text::The text shown during mouseover the editable text (default: "Click to edit")

 

哎……早知道就自己查查类库了,省得一开始还弄的那么麻烦,还想着单独建立一个controller……

在view里面:

<span id="xxx"><%= 需要现场编辑的内容%></span>
<%= in_place_editor "xxx",:url=>{用来处理提交数据的方法}%>
 类似资料: