如何从 Rails 3 中的 erb.js 输出助手?

发布于 2024-11-03 21:21:58 字数 1005 浏览 1 评论 0原文

这是我在 erb.js 文件中的内容......但它不起作用:

  1 //Update with a message
  2 $('#follow-update').html("<%= follow_button(@question) %>")

follow_button 是一个助手:

20   def follow_button(resource)
 21     
 22     type = resource.class.name
 23     follow_status = current_user.following?(resource)
 24 
 25     verb = "Follow" if follow_status == false
 26     verb = "Unfollow" if follow_status == true
 27 
 28     content_tag(:div, :class => "follow-button") do
 29       link_to "#{verb} this #{type} ", follow_path(:followed_type => type,
 30                                                   :followed_id => resource.id,
 31                                                   :follow_verb => verb), 
 32                                                   :class => "button",
 33                                                   :remote => true
 34     end
 35   end
 36 end

助手独立工作正常,但我希望它更新我的按钮。也许有一种方法可以只更新文本而不替换整个助手?

This is what I have in my erb.js file....but it doesn't work:

  1 //Update with a message
  2 $('#follow-update').html("<%= follow_button(@question) %>")

follow_button is a helper:

20   def follow_button(resource)
 21     
 22     type = resource.class.name
 23     follow_status = current_user.following?(resource)
 24 
 25     verb = "Follow" if follow_status == false
 26     verb = "Unfollow" if follow_status == true
 27 
 28     content_tag(:div, :class => "follow-button") do
 29       link_to "#{verb} this #{type} ", follow_path(:followed_type => type,
 30                                                   :followed_id => resource.id,
 31                                                   :follow_verb => verb), 
 32                                                   :class => "button",
 33                                                   :remote => true
 34     end
 35   end
 36 end

The helper stand-alone works fine, but I want it to update my button. Perhaps there's a way to just update the text without replacing the whole helper?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

梦巷 2024-11-10 21:21:58

创建一个仅包含您的助手的部分。

例如: '/wherever/_follow_button.html.erb'

在里面,调用你的助手:

<%= follow_button(question) %>

接下来,使用 JS 渲染它。

$('#some-id').html("<%= escape_javascript(render('/wherever/follow_button', :question => @question)) %>");

有一些方法可以提高效率,但希望这对现在有帮助。

Create a partial that holds your contains only your helper.

For example: '/wherever/_follow_button.html.erb'

Inside, call your helper:

<%= follow_button(question) %>

Next, render it using JS.

$('#some-id').html("<%= escape_javascript(render('/wherever/follow_button', :question => @question)) %>");

There are ways to make this more efficient but hopefully this helps for now.

李白 2024-11-10 21:21:58

您将需要 escape_javascript

$('#follow-update').html("<%= raw( escape_javascript( follow_button(@question ))) %>");

如果这不起作用,请参阅显示返回的 ajax 文本和按钮 html

You will need escape_javascript

$('#follow-update').html("<%= raw( escape_javascript( follow_button(@question ))) %>");

If this doesn't work, please see show the returned ajax text and the button html

靖瑶 2024-11-10 21:21:58

您无法从 js.erb 文件调用辅助方法,除非您按照 在 javascript 资源中使用 Rails 辅助方法 。例如:

<% environment.context_class.instance_eval { include ApplicationHelper } %>

您也应该小心转义 HTML。

You can't call a helper method from a js.erb file unless you explicitly include it as described in Using a Rails helper method within a javascript asset . For example:

<% environment.context_class.instance_eval { include ApplicationHelper } %>

And you should be careful about escaping your HTML too.

落花浅忆 2024-11-10 21:21:58

抱歉,我现在无法测试,但是您是否注意过 html 安全或 javascript 转义?

例如,尝试将您的 follow_button(@question) 包含在 raw()escape_javascript() 中?

Sorry, I cannot test right now, but have you paid attention to html safe or javascript escapes?

e.g. try to enclose your follow_button(@question)in raw() or escape_javascript()?

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