这是场景。
Summary:我有一个解决方案,但我想知道这是否是在 Rails 3 中使用 AJAX 回调的最佳方法。
Problem:我有一个链接,当单击它时,它应该使用 AJAX 来更新现有的 html 元素(在我的例子中是div
)页面上:
<div id="project_content">Change Goes here</div>
我的解决方案
In my projects/show.html.erb
true} %>
我的任务控制器如下:
class TasksController < ApplicationController
def index
respond_to do |format|
format.html
format.js { render action: "index", script: true }
end
end
end
并在我的tasks/index.js.erb
I have
$("#project_content").html(" 'tasks')%>");
问题:这一切都有效。但为什么我必须做这一切?是否有(或没有)一个解决方案可以让我们所有人简单地做到这一点
<%= link_to "tasks", project_tasks_path(@project), {:remote=>true, :update=>"project_content"} %>
然后这将加载tasks/index.js.erb
进入关键的任何元素:update
参考?