Rails 3.0 / JQuery - 在 ajax 调用上交换部分
我有两个部分 _choose.html.erb 和 _details.html.erb。
我像这样渲染选择:
<div id="choose" class="box" style="padding:10px;" >
<%= render :partial => "choose" %>
</div>
在控制器中我调用选择方法。
def choose
respond_to do |format|
format.js
end
end
在 choice.js.erb 中,我调用以下内容:
$("#choose").replaceWith(<%= escape_javascript(render(:partial => "details"))%>);
但没有任何反应。详细信息部分位于正确的位置,如果我在页面中渲染它,就像这样 <%= render :partial => “详情”%>,弹出来就没有问题了。但我无法使用上面的ajax方法来交换它。
如果我这样做:
$("#choose").replaceWith("a yaddy yaddy");
“a yaddy yaddy”将在选择的 div 中呈现。
我在这里错过了什么吗?
我一直在阅读,包括SO,但无济于事。
更新
响应是 200
或 304
。使用 firebug,我可以看到响应包含部分内容,但这没有在浏览器中呈现,这很奇怪,但我想我已经犯了一些我不知道的偷偷摸摸的不满......
更新
选择.js.erb:
如果我调用 "$("#choose").html("<%= escape_javascript(render(:partial => "choose", :object => @pages))%>")"
选择部分会使用页面实例变量进行更新。
谢谢你们。
I have two partials _choose.html.erb and _details.html.erb.
I render choose like so:
<div id="choose" class="box" style="padding:10px;" >
<%= render :partial => "choose" %>
</div>
In the controller I call the choose method.
def choose
respond_to do |format|
format.js
end
end
And in choose.js.erb I call the following:
$("#choose").replaceWith(<%= escape_javascript(render(:partial => "details"))%>);
But nothing happens. The details partial is in the correct place and if I render it in the page, like so <%= render :partial => "details" %>, up it pops no problem. But I am unable to swap it using the above ajax method.
If I do:
$("#choose").replaceWith("a yaddy yaddy");
"a yaddy yaddy" is rendered in the choose div.
Am I missing something here?
I have been reading around, including SO, but to no avail.
Update
The response is either a 200
or a 304
. And using firebug, I can see that the response contains the partial contents but this is not rendered in the browser, which is strange, but I'd imagine I have committed some sneaky grievance I'm unaware of....
Update
in choose.js.erb:
If I call "$("#choose").html("<%= escape_javascript(render(:partial => "choose", :object => @pages))%>")"
the choose partial is updated with the pages instance variable.
Thanks, y'all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能确保你的两个部分都有一个
id = "choose"
div 吗?如果没有,您的replaceWith
将删除它们。您可能想要.html("")
吗?Can you make sure that both of your partials have a
id = "choose"
div in them? If not, yourreplaceWith
is dropping them. Could you possible want.html("")
?