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?

haroldcampbell
  • 1,512
  • 1
  • 14
  • 22

1 Answers1

0

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.

Nash Bridges
  • 2,350
  • 14
  • 19
  • I don't understand what you are saying? – haroldcampbell Mar 13 '12 at 16:55
  • @haroldcampbell Spend some time examing `link_to_remote` functionality. It even has the `:update` option which you propose. – Nash Bridges Mar 13 '12 at 18:39
  • But `link_to_remote` was removed from Rails 3+: With that said the questions remains. What's the replacement for `link_to_remote` where I can simply supply an `:update` hash? – haroldcampbell Mar 14 '12 at 00:15
  • 1
    @haroldcampbell Your question was "WHY do I have to do all of this?". And the answer is because [unobtrusive javascript](http://en.wikipedia.org/wiki/Unobtrusive_JavaScript) is the right way to do things. That's why `link_to_remote` is gone in Rails 3. – Nash Bridges Mar 14 '12 at 07:28