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.
我希望在ruby on rails教程中实现ajax,控制器将在处理ajax请求时返回一个对象。我不知道如何处理javascript文件中的响应。我想基于在javascript文件中返回的对象更新一些div。
Here is what I have written in my 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?
怎么做呢?
3 个解决方案
#1
3
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 });
在rails中,当您向链接添加:remote =>时,它基本上负责jquery ajax调用的第一部分。这就是为什么你不需要绑定。(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. So anything in your javascript erb template is put in the callback like this
Rails负责第一部分,直到.done回调。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
.
格式之后不需要任何东西。因为此时的默认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. Rails makes using ajax very simple. I hope that helps.
现在您知道,当单击该链接时,它将直接呈现showcomment模板,其中的任何javascript都将立即运行。Rails使得使用ajax非常简单。我希望有帮助。
#2
2
change showcomment to this:
showcomment改成这样的:
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
. A sample use is as below:
在showcomments.js。erb可以访问两个对象@article和@comment。样品使用如下:
$('#yourdiv').html('<%= @article.name %>');
#3
0
You render nothing when responding to js format. So code in showcomments.js.erb
isn't evaluated. Remove { render :nothing => true }
in controller and do whatever you want in showcomments.js.erb
. BTW you don't need ajax:success
event. It is the end of request already.
当响应js格式时,您不呈现任何内容。所以showcomments.js代码。erb不是评估。删除控制器中的{render:nothing => true},并在showcomments.js.erb中执行所需的操作。顺便说一句,您不需要ajax:success event。请求已经结束了。
showcomments.js.erb
showcomments.js.erb
$('.show_comment').closest('tr').html("<%= # you may render partial here or whatever %>");
#1
3
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 });
在rails中,当您向链接添加:remote =>时,它基本上负责jquery ajax调用的第一部分。这就是为什么你不需要绑定。(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. So anything in your javascript erb template is put in the callback like this
Rails负责第一部分,直到.done回调。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
.
格式之后不需要任何东西。因为此时的默认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. Rails makes using ajax very simple. I hope that helps.
现在您知道,当单击该链接时,它将直接呈现showcomment模板,其中的任何javascript都将立即运行。Rails使得使用ajax非常简单。我希望有帮助。
#2
2
change showcomment to this:
showcomment改成这样的:
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
. A sample use is as below:
在showcomments.js。erb可以访问两个对象@article和@comment。样品使用如下:
$('#yourdiv').html('<%= @article.name %>');
#3
0
You render nothing when responding to js format. So code in showcomments.js.erb
isn't evaluated. Remove { render :nothing => true }
in controller and do whatever you want in showcomments.js.erb
. BTW you don't need ajax:success
event. It is the end of request already.
当响应js格式时,您不呈现任何内容。所以showcomments.js代码。erb不是评估。删除控制器中的{render:nothing => true},并在showcomments.js.erb中执行所需的操作。顺便说一句,您不需要ajax:success event。请求已经结束了。
showcomments.js.erb
showcomments.js.erb
$('.show_comment').closest('tr').html("<%= # you may render partial here or whatever %>");