【问题标题】:Rails: render partial in .coffee fileRails:在 .coffee 文件中渲染部分内容
【发布时间】:2017-01-31 19:28:44
【问题描述】:

当.change 事件发生时,我想使用coffeescript 到.append 部分。问题是当.change 事件发生时,下面的代码不会呈现部分,而是<%= j render(:partial => 'participant_data').html_safe %> 在网站上显示为文本。

$('input[type=number][id=quantity-select]').change ->
  $('#insert-here').append('<%= j render(:partial => \'participant_data\') %>')
  return

当我运行$('#insert-here').append("Test") 时,页面上会显示Test 这个词。所以我的目标是正确的 div 并且能够 append 文本,但我想渲染部分而不是文本。

请告诉我我需要如何更改代码以便部分呈现。

【问题讨论】:

  • 我没有用咖啡工作,但 js.erb 看起来不错。你能告诉我们你的咖啡文件和控制器吗?

标签: javascript ruby-on-rails ruby-on-rails-4 coffeescript


【解决方案1】:

尝试关注。

$('#insert-here').append("#{escape_javascript(render partial: 'participant_data')}")

更新答案:尝试使用单引号。

$('#insert-here').append('<%= j render(:partial => \'participant_data\') %>')

参考:

How to escape while rendering a js.coffee file in response to an action

【讨论】:

  • 当我使用此代码时,我收到以下错误消息:ReferenceError: escape_javascript is not defined
  • 感谢您的更新。当我运行更新的代码时,我收到以下错误ExecJS::RuntimeError SyntaxError: [stdin]:50:57: unexpected identifier
猜你喜欢
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 2023-03-13
相关资源
最近更新 更多