使用Rails 3 link_to进行Ajax回调的正确方法是什么 [英] What's the correct way to do ajax callback with Rails 3 link_to

查看:79
本文介绍了使用Rails 3 link_to进行Ajax回调的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景.

摘要:我有一个解决方案,但是我想知道这是否是在Rails 3中运行AJAX回调的最佳方法.

Summary: I have a solution, but I want to know if this is the best way to get an AJAX callback working in Rails 3.

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

Problem: I have a link that when clicked it should use AJAX to update an existing html element (in my case a div) on the page:

< div id ="project_content">此处更改为</div>

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

true}%>

我的TasksController如下:

My TasksController is as follows:

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(<%= escape_javascript(render:partial =>'tasks')%>");

$("#project_content").html("<%= escape_javascript(render :partial => 'tasks')%>");

问题:这一切都可以.但是为什么我必须做所有这一切?不是一个(或没有一个)解决方案,它使我们所有人都可以简单地做到这一点

Question: This all works. But WHY do I have to do all of this? Wasn't there (or isn't there) a solution that used to all us to simply do this

<%=链接到任务",project_tasks_path(@project),{:remote => true,:update =>"project_content"}%>

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

And this would then load tasks/index.js.erb into whichever element the key :update references?

推荐答案

您正试图重塑

You're attempting to reinvent link_to_remote from old Rails 2.3. Such a solution in general comes out to be too complex and rigid.

这篇关于使用Rails 3 link_to进行Ajax回调的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆