RoR
中提供了一些方法來簡化
Ajax
的使用。
1.??
Javascript_include_tag
這是一個引入
javascript
文件的方法
,
如果使用
<%=javascript_include_tag(:defaults)%>
那么將引入
controls.js,dragdrop.js,effects.js,prototype.js
這四個
js
文件。使用
<%=javascript_include_tag(“common”)%>
將引入在
public/javascripts/
下的
common.js
文件。
?
2.??
link_to_remote
這是一個很方便使用的方法,它可以返回一段
html
代碼段然后更新一個區(qū)域。例子:
??? <%=
link_to_remote
(
"
編輯
"
,
:update
=>
"type"
,
:url
=>{
:controller
=>
"types"
,
:action
=>
"edit"
,
:id
=>type.id}%>
?
<div id=”type”>
</div>
?
:update
對應(yīng)的值就是要更新的區(qū)域,
: controller
對應(yīng)的是控制類,
:action
對應(yīng)的是執(zhí)行控制類中的那個方法。
:id
是要傳入的參數(shù)。
?
也可以用回掉方法來接受返回回來的
html
代碼或者是
xml.
例子:
??? <%=
link_to_remote
(
"
編輯
"
,
:url
=>{
:action
=>
"edit"
,
:id
=>submodule.id},
????????????
:complete
=>
"showEditModuleDialog(request)"
)%>
?
function
showEditModuleDialog
(request){
???? alert(request.responseText);
???? alert(request.responseXML);
}
?
3.??
form_remote_tag
這個方法可以使你提交一個
form
而不用提交整個頁面。例子:
<%=form_remote_tag(
:update
=>
"bugsetting"
,
:url
=>{
:controller
=>
"systems"
,?
:action
=>
"create"
})%>
?
??
<table>
???
<tr>
???????
<td>
操作系統(tǒng)名稱
:
</td>
???????
<td>
<%= text_field 'system', 'name'? %></td>
???
</tr>
???
<tr>
???????
<td>
錯誤等級說明
:
</td>
???????
<td>
<%= text_field 'system', 'description'? %></td>
???
</tr>
???
<tr>
???????
<td
colspan
="2">
???????????
<input
type
="submit" value="
保存
"
/>
???????
</td>
???
</tr>
</table>
<%= end_form_tag %>
這邊參數(shù)的使用和
link_to_remote
的使用是一樣的。