admin管理员组

文章数量:1335887

Hi want to implement ajax in my ruby on rails tutorials and the controller will return an object on handling the ajax request. I dont know to handle the response in my javascript file. I want to update some div based on object returned in javascript file.

Here is what I have written in my showments.js.erb

$('.show_ment').bind('ajax:success', function() {
    $(this).closest('tr')="Something here from the object returned");
});

My link where ajax call is called is via this line of code

 <td><%= link_to 'Show Comment', :action => 'showment' , :id =>article, :remote => true ,:class=>'show_ment' %></td>

My controller action where this request is handled is like this

def showment
   @article = Article.find(params[:article_id])
   @ment = @articlements.find(params[:id])
    respond_to do |format|
      format.js{ render :nothing => true }
    end
end

How can this be done?

Hi want to implement ajax in my ruby on rails tutorials and the controller will return an object on handling the ajax request. I dont know to handle the response in my javascript file. I want to update some div based on object returned in javascript file.

Here is what I have written in my showments.js.erb

$('.show_ment').bind('ajax:success', function() {
    $(this).closest('tr')="Something here from the object returned");
});

My link where ajax call is called is via this line of code

 <td><%= link_to 'Show Comment', :action => 'showment' , :id =>article, :remote => true ,:class=>'show_ment' %></td>

My controller action where this request is handled is like this

def showment
   @article = Article.find(params[:article_id])
   @ment = @article.ments.find(params[:id])
    respond_to do |format|
      format.js{ render :nothing => true }
    end
end

How can this be done?

Share Improve this question asked Jul 18, 2014 at 12:02 user3726986user3726986
Add a ment  | 

3 Answers 3

Reset to default 4

I just wanted to try and expand on the other answers a little bit.

In rails when you add :remote => true to a link it essentially takes care of the first part of a jquery ajax call. That's why you don't need bind.(ajax:success, function(){ # do stuff here });

This is a typical jquery ajax call

$.ajax({
  url: "test.html",
  type: "POST"
}).done(function() {
  $( this ).addClass( "done" );
});

Rails takes care of the first part, up to the .done callback. So anything in your javascript erb template is put in the callback like this

.done(function() {
  #your showments.js.erb file is inserted here
});

To render the showments.js.erb template from the controller just do this

def showment
  @article = Article.find(params[:article_id])
  @ment = @article.ments.find(params[:id])
  respond_to do |format|
    format.js
  end
end

You don't need anything after format.js because the default rails action at that point is to render a js template with the same name as the action, in this case it's showment.js.erb.

Now you know when that link is clicked it will go straight to rendering that showment template , and any javascript in there will be run instantly. Rails makes using ajax very simple. I hope that helps.

change showment to this:

def showment
   @article = Article.find(params[:article_id])
   @ment = @article.ments.find(params[:id])
    respond_to { |format| format.js }
end 

In showments.js.erb you can access both objects @article and @ment. A sample use is as below:

$('#yourdiv').html('<%= @article.name %>');

You render nothing when responding to js format. So code in showments.js.erb isn't evaluated. Remove { render :nothing => true } in controller and do whatever you want in showments.js.erb. BTW you don't need ajax:success event. It is the end of request already.

showments.js.erb

$('.show_ment').closest('tr').html("<%= # you may render partial here or whatever %>");

本文标签: javascriptAjax success callback in ruby on railsStack Overflow