Rails RJS 模板显示代码而不是 html 页面
在谷歌搜索并为此伤了我几个小时之后,我终于决定发布这个问题。这是代码...
view1.html.erb
--------------
<%=link_to_remote_redbox "Link", :url => {:action => :action1, :id => @some.id}
some_controller.rb
------------------
def action1
render :layout => false
end
def action2
do some processing
end
action1.html.erb
--------------------
<form onsubmit="new Ajax.Request('/some_controller/action2', {asynchronous:true, evalScripts:true, onComplete:function(request){RedBox.close(); return false;}, parameters:Form.serialize(this)}); return false;}" method="post" action="/some_controller/action2">
<input type=text name='username'>
<input type='submit' value='submit'>
</form>
action2.rjs
-----------
page.replace_html("some_div", (render(:partial => "some_partial")))
当action2.rjs启动时,该代码应该显示html页面,而不是我得到这个
Element.update("some_div", "<style type=\"text/css\">\n\n..............
正如我阅读的其他帖子上的建议,他们说这是由于“:update => some_div”引起的“在 link_to_remote_redbox 函数中,但显然我的代码没有这个。
帮助总是值得赞赏的。 非常感谢
After having Googled and hurting my brain for hours on this, I finally decided to post this question. Here's the code...
view1.html.erb
--------------
<%=link_to_remote_redbox "Link", :url => {:action => :action1, :id => @some.id}
some_controller.rb
------------------
def action1
render :layout => false
end
def action2
do some processing
end
action1.html.erb
--------------------
<form onsubmit="new Ajax.Request('/some_controller/action2', {asynchronous:true, evalScripts:true, onComplete:function(request){RedBox.close(); return false;}, parameters:Form.serialize(this)}); return false;}" method="post" action="/some_controller/action2">
<input type=text name='username'>
<input type='submit' value='submit'>
</form>
action2.rjs
-----------
page.replace_html("some_div", (render(:partial => "some_partial")))
with that code in place when action2.rjs kicks in it should display the html page instead I am getting this
Element.update("some_div", "<style type=\"text/css\">\n\n..............
As suggested on other posts I read, they say its caused because of the ":update => some_div" in the link_to_remote_redbox function but clearly my code doesn't have that.
Help is always appreciated.
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保布局包含默认的 javascript 文件。换句话说:
Make sure the layout includes the default javascript files. In other words:
尝试以下操作:-
或
Try following:-
OR
如果你想从浏览器调用 return ajax 调用,Salil 的方法会起作用。但是如果你想调用更复杂的 javascript(这可能会在你的控制器中造成混乱),你可以这样做:
这将明确告诉 Rails 返回javascript 而不是 html。如果您使用 jQuery 而不是 RJS,这也很有用。
干杯!
Salil's method would work if you want to call return an ajax call from your browser.. but in case you want to call more complex javascript (which could be messy in your controller) you can do this instead:
This will explicitly tell rails to return javascript rather than html. This is also useful if you use jQuery rather than RJS.
Cheers!