Rails RJS 模板显示代码而不是 html 页面

发布于 2024-08-28 01:05:46 字数 1092 浏览 8 评论 0原文

在谷歌搜索并为此伤了我几个小时之后,我终于决定发布这个问题。这是代码...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿得七秒忆 2024-09-04 01:05:46

确保布局包含默认的 javascript 文件。换句话说:

Make sure the layout includes the default javascript files. In other words:

口干舌燥 2024-09-04 01:05:46

尝试以下操作:-

def action2
  render :update do|page|
    page.replace_html, 'some_div', :partial => 'some_partial'
  end
end

<form onsubmit="new Ajax.Request('', {asynchronous:true, evalScripts:true, onComplete:function(request){RedBox.close(); return false;}, parameters:Form.serialize(this)}); return false;}" method="post" action="/some_controller/action2">

Try following:-

def action2
  render :update do|page|
    page.replace_html, 'some_div', :partial => 'some_partial'
  end
end

OR

<form onsubmit="new Ajax.Request('', {asynchronous:true, evalScripts:true, onComplete:function(request){RedBox.close(); return false;}, parameters:Form.serialize(this)}); return false;}" method="post" action="/some_controller/action2">
身边 2024-09-04 01:05:46

如果你想从浏览器调用 return ajax 调用,Salil 的方法会起作用。但是如果你想调用更复杂的 javascript(这可能会在你的控制器中造成混乱),你可以这样做:

def action2
  ...some code
  respond_to do |format|
    format.js
  end
end

这将明确告诉 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:

def action2
  ...some code
  respond_to do |format|
    format.js
  end
end

This will explicitly tell rails to return javascript rather than html. This is also useful if you use jQuery rather than RJS.

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文