怎样做Ajax回调使用Rails 3的link_to的正确方法 [英] What's the correct way to do ajax callback with Rails 3 link_to

查看:154
本文介绍了怎样做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的

  

$(#project_content)HTML(<%= escape_javascript(渲染:部分=>'任务')%>);

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

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

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

解决方案

我找到了答案<一href="http://stackoverflow.com/questions/5511787/rails-2-to-rails-3-using-link-to-instead-of-link-to-remote-including-remote-a">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 Solution In my projects/show.html.erb

true} %>

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

$("#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

<%= 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的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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