怎样做Ajax回调使用Rails

怎样做Ajax回调使用Rails

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

问题描述

下面的情景。

摘要:我有一个解决方案,但我想知道这是否是获得一个AJAX回调Rails 3中工作的最好方法。

问题:我有一个链接,点击时,应该使用AJAX来更新现有的HTML元素(在我的情况下, DIV )的网页上:

< D​​IV ID =project_content>迭出此处< / DIV>

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

我的TasksController如下:

 类TasksController<的ApplicationController
    高清指数
        respond_to代码做|格式|
              的format.html
              format.js {渲染的动作:指数,剧本:真}
        结束
    结束
结束
 

和我的任务/ index.js.erb的

问:这一切工作。但为什么我必须做的这一切?是不存在(或不存在),其用于所有的解决方案,我们只需做到这一点

<%=的link_to任务,project_tasks_path(@project),{:远程=>真,:更新=>中project_content}%>

和那么这将载入任务/ index.js.erb的进入任何元素的关键:更新引用?

解决方案

我找到了答案here

这是可悲的,这个功能从Rails 3中删除。

Here's the scenario.

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.

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">Change Goes here</div>

My SolutionIn my projects/show.html.erb

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

and in my tasks/index.js.erb I have

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

<%= link_to "tasks", project_tasks_path(@project), {:remote=>true, :update=>"project_content"} %>

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

解决方案

I found the answer here

It's sad that this feature was removed from Rails 3.

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

07-24 03:04