Ruby 的助手
我正在为一些剪贴板复制代码制作一个助手。我希望剪贴板复制视图中生成的 url。
例如,以下是在视图中有效的链接:
<%= link_to event_path(event.id) %>
它在视图中工作得很好。我很难弄清楚该把它放在哪里作为助手。我在助手中传递一个变量,那么我应该将其放入视图中并将其传递给变量,还是应该将其放入助手中?现在它在助手中,我得到正常的文本。
这是 helper.rb 文件:
def clipboard_copy(text = nil, bgcolor='#FFFFFF')
html = <<-EOF
...
<param NAME="FlashVars" value="text=#{text}">
...
FlashVars="text=#{text}"
...
EOF
html.html_safe
end
我从视图中调用它:
<%= clipboard_copy %>
谢谢!
I am making a helper for some clipboard copy code. I would like the clipboard to copy a url that is generated in the view.
For example, here is the link that works in the view:
<%= link_to event_path(event.id) %>
It works just fine in the view. I am having a hard time figuring out where to put it for the helper. I am passing a variable in my helper, so should I put it in the view and pass it to the variable or should I put it in the helper? Right now it's in the helper and I get normal text back.
Here is the helper.rb file:
def clipboard_copy(text = nil, bgcolor='#FFFFFF')
html = <<-EOF
...
<param NAME="FlashVars" value="text=#{text}">
...
FlashVars="text=#{text}"
...
EOF
html.html_safe
end
and I am calling it from the view with:
<%= clipboard_copy %>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来似乎并不真正需要辅助方法 - 您是否尝试过将其放入部分中,将文本(以及我猜的背景颜色)作为局部变量传递给该部分?
This looks like it doesn't really need a helper method - have you tried putting it in a partial, passing the text (and, I guess, the bgcolor) as locals to that partial?