What's the correct way to do ajax callback with Rails 3 link_to [duplicate]

倖福魔咒の 提交于 2019-12-07 15:39:55

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9655142/whats-the-correct-way-to-do-ajax-callback-with-rails-3-link-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!