Mis*_*hko 14 ajax ruby-on-rails ruby-on-rails-3
我想在我的页面上有一个链接(有更好的选项吗?),点击时会发出Ajax请求.(我想在点击链接时更新数据库中的字段.)
实现这一目标的最简单方法是什么?
你能给我推荐一些教程吗?
Jin*_*Kim 24
真的很简单.在您的视图中,有一个像这样的链接/按钮.重要的是:remote => true
<%= link_to 'Update Thingy', update_thingy_path, :confirm => 'You sure you wanna update?', :remote => true %>
Run Code Online (Sandbox Code Playgroud)
要么
<%= button_to('Update Thingy', {:action => 'update_thingy', :thingyid => 314}, :method => :get, :remote => true) %>
Run Code Online (Sandbox Code Playgroud)
显然,你必须得到update_thingy_path以正常解决某些操作.不同之处在于渲染时您将渲染一些*.js.erb而不是*.html.erb.在那个update_thingy.js.erb中,你只需要在客户端中运行你想要运行的任何javascript.您可能想要通知用户更新发生在例如(在jQuery中):
$('#notice').html("Thingy was update.")
Run Code Online (Sandbox Code Playgroud)
或者,如果您返回的任何javascript非常简单,在控制器中您可以执行以下操作,而不是使用整个js.erb进行单行程.
render :js => "alert('Blah')"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11760 次 |
| 最近记录: |