What's the correct way to do ajax callback using Rails 3 link_to

Here's the script.

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, 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 myprojects/show.html.erb

true}%>

My TasksController looks like this:

class TasksController < ApplicationController
    def index
        respond_to do |format|
              format.html
              format.js { render action: "index", script: true }
        end
    end
end

      

and in mine tasks/index.js.erb

I have

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

Question: It all works. But WHY should I do all this? There was (or is not) a solution used for all of us to just do this

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

And then it will be loaded tasks/index.js.erb

into whatever element it refers to?

+3


source to share


2 answers


I found the answer here



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

0


source


You are trying to reinvent link_to_remote from old Rails 2.3. In the general case, such a solution turns out to be too complicated and tough.



0


source







All Articles