What's the correct way to do ajax callback wit

2019-05-09 16:24发布

问题:

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.



回答2:

I found the answer here

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