[英]Ajax success callback in ruby on rails
Hi want to implement ajax in my ruby on rails tutorials and the controller will return an object on handling the ajax request. 嗨,我想在我的ruby on rails教程中实现ajax,控制器将在处理ajax请求时返回一个对象。 I dont know to handle the response in my javascript file. 我不知道要处理我的JavaScript文件中的响应。 I want to update some div based on object returned in javascript file. 我想基于javascript文件中返回的对象更新一些div。
Here is what I have written in my showcomments.js.erb 这是我在showcomments.js.erb中编写的内容
$('.show_comment').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 我的调用ajax的链接是通过以下代码行
<td><%= link_to 'Show Comment', :action => 'showcomment' , :id =>article, :remote => true ,:class=>'show_comment' %></td>
My controller action where this request is handled is like this 我处理该请求的控制器动作是这样的
def showcomment
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
respond_to do |format|
format.js{ render :nothing => true }
end
end
How can this be done? 如何才能做到这一点?
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. 在rails中,当您将:remote => true
添加到链接时,它实际上会处理jquery ajax调用的第一部分。 That's why you don't need bind.(ajax:success, function(){ # do stuff here });
这就是为什么不需要bind.(ajax:success, function(){ # do stuff here });
This is a typical jquery ajax call 这是典型的jquery ajax调用
$.ajax({
url: "test.html",
type: "POST"
}).done(function() {
$( this ).addClass( "done" );
});
Rails takes care of the first part, up to the .done
callback. Rails负责第一部分,直到.done
回调。 So anything in your javascript erb template is put in the callback like this 因此,您的javascript erb模板中的所有内容都将像这样放在回调中
.done(function() {
#your showcomments.js.erb file is inserted here
});
To render the showcomments.js.erb template from the controller just do this 要从控制器渲染showcomments.js.erb模板,只需执行此操作
def showcomment
@article = Article.find(params[:article_id])
@comment = @article.comments.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 showcomment.js.erb
. 在format.js之后,您不需要任何内容,因为此时的默认rails动作是呈现与动作名称相同的js模板,在本例中为showcomment.js.erb
。
Now you know when that link is clicked it will go straight to rendering that showcomment
template , and any javascript in there will be run instantly. 现在,您知道单击该链接时,它将直接渲染该showcomment
模板,并且其中的任何JavaScript都将立即运行。 Rails makes using ajax very simple. Rails使使用ajax非常简单。 I hope that helps. 希望对您有所帮助。
change showcomment to this: 更改对此的评论:
def showcomment
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
respond_to { |format| format.js }
end
In showcomments.js.erb you can access both objects @article
and @comment
. 在showcomments.js.erb中,您可以访问@article
和@comment
对象。 A sample use is as below: 用法示例如下:
$('#yourdiv').html('<%= @article.name %>');
You render nothing when responding to js format. 响应js格式时,您不呈现任何内容。 So code in showcomments.js.erb
isn't evaluated. 因此,不会评估showcomments.js.erb
代码。 Remove { render :nothing => true }
in controller and do whatever you want in showcomments.js.erb
. 删除控制器中的{ render :nothing => true }
并在showcomments.js.erb
执行任何showcomments.js.erb
。 BTW you don't need ajax:success
event. 顺便说一句,您不需要ajax:success
事件。 It is the end of request already. 已经是请求结束了。
showcomments.js.erb showcomments.js.erb
$('.show_comment').closest('tr').html("<%= # you may render partial here or whatever %>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.