开发者

Rails - How to render js in an html response

开发者 https://www.devze.com 2023-01-28 14:06 出处:网络
I have an action \'index\', that when called via ajax, renders index.js which renders a partial: $(\"#content\").html(\"<%= escape_javascript(render :partial => \'index\') %>\");

I have an action 'index', that when called via ajax, renders index.js which renders a partial:

$("#content").html("<%= escape_javascript(render :partial => 'index') %>");

How do I make the same thing happen in the html response? I tried:

format.html { render "i开发者_JS百科ndex.js" }

And although I don't get any errors, the js code doesn't seem to run, because the partial doesn't render.


Have you tried wrapping the script inside <script> tag?

Your index.html.erb would look like this:

<script type="text/javascript">
  $("#content").html("<%= escape_javascript(render :partial => 'index') %>");
</script>


The default render is a shortcut for render :action => :action_name. To specify a filesystem path use render :file => "index.js"

0

精彩评论

暂无评论...
验证码 换一张
取 消