Rails, jquery and Ajax

How do I make an ajax call and then update a div with a partial? I've tried this but it's not working. Comment is created but the partial is not loaded.

//view       <a href="#" id="testlink">Testlink</a>       <div id="commentlist">

      </div>

//controller   def new

      @comment = Comment.new       @comment.save

      respond_to do |format|         format.html { }         format.js { }       end

  end

//js $(function() {

  $('#testlink').click(function() {     $.ajax({        type: "GET",        url: "/comments/new",        success: function(){         $('#commentlist').html('<%= render :partial => "comments" %>')

       }     });   }); });

Why don't you use remote_function() ?

- Chirag Shah

Chirag Shah wrote in post #1013312:

Why don't you use remote_function() ?

- Chirag Shah

I used to do that but now I'm trying to do it with jquery.

For work with jquery and rails function you can use jrails plugin

- Chirag Shah

Paul Bergstrom wrote in post #1013320:

One way:

link_to "#", new_comment_path, :remote => true, :id => "new-comment-link"

new.js.erb

$(#new-comment-link).hide(); $('#commentlist').html('<%= render :partial => "comments" %>')

You can use ruby logic in the .js.erb file - just like you could in former .rjs files.

<% if @condition %> alert("condition <%= @condition %>"); <% end %>

Cheers, Eric

Railscasts has some examples:

One way:

link_to "#", new_comment_path, :remote => true, :id => "new-comment-link"

new.js.erb

$(#new-comment-link).hide(); $('#commentlist').html('<%= render :partial => "comments" %>')

You can use ruby logic in the .js.erb file - just like you could in former .rjs files.

<% if @condition %> alert("condition <%= @condition %>"); <% end %>

Cheers, Eric

"Eric Björkvall" <eric.bjorkvall@gmail.com> wrote in post #1013626:

One way:

link_to "#", new_comment_path, :remote => true, :id => "new-comment-link"

new.js.erb

$(#new-comment-link).hide(); $('#commentlist').html('<%= render :partial => "comments" %>')

You can use ruby logic in the .js.erb file - just like you could in former .rjs files.

<% if @condition %> alert("condition <%= @condition %>"); <% end %>

Cheers, Eric

Thanks. But are you sure this works? Doesn't work here.

If I have <%= link_to 'Test', { :action => 'test' }, :remote => true %> it actually goes to the action not making an ajax call.

What do you mean by that? It *should* go to the action, as an ajax call.

Colin

@Paul Bergstrom :

In rails 3, handleling an Ajax request is too easy with Ujs. I’ll try to give you a good example. I suppose, i want to ‘activate’ post an article via an Ajax call :

I have :

  • posts_controller

  • Route match for activating in Routes.rb file

  • Views/posts/ directory where views related t o ‘posts’ are located.

  • And of course a model (optional for our case) :slight_smile:

  • Suppose that i have a confirm link that’s alow me to ‘activate’ a particular ‘post’. <%= link_to ‘activate’ , {:controller => “posts”, :action => “activate”, :id => post.id}, :remote => true %>

  • I also have in Routes.rb file this satement : match “posts/activate/:id” => “posts#activate”

  • I have in the Posts_controller file the action ‘activate’ : def activate

    respond_to do |format| format.js { } end end

  • And finally, you have to create a file named ‘activate.js.erb’ and put this test line of code for ajax responding :

    alert(“it’s work :D”);

Just try this for your case an keep me update.

Good luck

Colin Law wrote in post #1017714:

It should go to the 'test' action. What happens then depends partly on what you do in that action.

Colin

Colin Law wrote in post #1017722:

Colin Law wrote in post #1017722:

So how do I make a basic ajax call from a link, do something in an action and update a div with new content?

If you want to follow the jqueryish way, then you setup a click handler on the link as your first post does, but your success function should be more along the lines of

function(data){    $('#commentlist').html(data) }

(which assumes that your action is producing a chunk of html in response)

Fred

lionel first-developer wrote in post #1017744:

@Paul Bergstrom :

Just try this for your case an keep me update.

Good luck

Thanks for the help.

I tried it. It makes a http request and goes to the url. No alert message. Strange.

Try using Firebug in Firefox to check there are no script errors. Then copy the complete html source from the page (View > Page Source or similar in browser) and paste it into the w3c html validator to check the html is ok.

Colin

lionel first-developer wrote in post #1017744:

@Paul Bergstrom :

In rails 3, handleling an Ajax request is too easy with Ujs. I'll try to

Btw, are you sure I don't need an extra gem for UJS?

This is what I've been doing for jquery:

layouts/application.html.erb:

<!DOCTYPE html> <html> <head>   <title><%= @title %></title>   <%= csrf_meta_tag %>

  <%=     javascript_include_tag(       "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js&quot;,       "jquery.rails.js"     )   %>

Then go here:

https://github.com/rails/jquery-ujs

and click on:

src/

then click on:

rails.js

Copy that whole file and paste it into a file that you name:

jquery.rails.js

and place that file in the directory:

public/javascripts

You should see rails.js in that directory, which is for prototype. Note how javascript_include_tag() links to jquery.rails.js.

yes do you have this in your Gemfile : gem 'jquery-rails' ?

and also have you run this command : ' rails g jquery:install ' , to install jquery require file ?

7stud -- wrote in post #1017753:

This is what I've been doing for jquery:

jquery.rails.js

Thank you. Now it works. :slight_smile:

So I've been sitting with this problem for a long time and this was what I missed. Did I miss it or was it poorly explained?