【问题标题】:What's the correct way to do ajax callback with Rails 3 link_to [duplicate]使用 Rails 3 link_to 进行 ajax 回调的正确方法是什么 [重复]
【发布时间】:2012-03-11 13:00:50
【问题描述】:

这是场景。

总结: 我有一个解决方案,但我想知道这是否是让 AJAX 回调在 Rails 3 中工作的最佳方式。

问题: 我有一个链接,点击它应该使用 AJAX 更新页面上现有的 html 元素(在我的情况下为 div):

<div id="project_content">Change Goes here</div>

我的解决方案 在我的projects/show.html.erb

真的} %>

我的TasksController如下:

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 我有

$("#project_content").html(" 'tasks')%>");

问题: 这一切都有效。但为什么我必须做这一切?不存在(或不存在)过去我们所有人都可以简单地执行此操作的解决方案

&lt;%= link_to "tasks", project_tasks_path(@project), {:remote=&gt;true, :update=&gt;"project_content"} %&gt;

然后这会将tasks/index.js.erb 加载到键:update 引用的任何元素中?

【问题讨论】:

    标签: ruby-on-rails ajax ruby-on-rails-3 link-to


    【解决方案1】:

    您正试图从旧的 Rails 2.3 中重塑 link_to_remote。这样的解决方案通常过于复杂和死板。

    【讨论】:

    • 我听不懂你在说什么?
    • @haroldcampbell 花一些时间检查link_to_remote 的功能。它甚至还有您建议的:update 选项。
    • link_to_remote 已从 Rails 3+ 中删除:话虽如此,问题仍然存在。 link_to_remote 的替代品是什么,我可以简单地提供 :update 哈希?
    • @haroldcampbell 你的问题是“为什么我必须做这一切?”。答案是因为unobtrusive javascript 是做事的正确方式。这就是为什么 link_to_remote 在 Rails 3 中消失了。
    猜你喜欢
    • 1970-01-01
    • 2011-06-13
    • 2018-07-08
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    相关资源
    最近更新 更多