Rails - 迭代一个字段中的数据库条目,以换行符分隔
在我的应用程序中,我有一个产品模型,其中包含四个图像路径字段。我用它来制作幻灯片。
然而,我希望将所有这些路径放在一个大文本字段中,并通过任何有效的方式将它们分开(换行符将是表单中最容易处理的)。
我在想这样的事情:
<% for ... in @screenshots %>
<%= lightbox_to(@product.screenshot, @product.screenshot, "screenshots") %>
<% end %>
并希望结果是:
<%= lightbox_to(@product.screenshot1, @product.screenshot1, "screenshots") %>
<%= lightbox_to(@product.screenshot2, @product.screenshot2, "screenshots") %>
<%= lightbox_to(@product.screenshot3, @product.screenshot3, "screenshots") %>
...
非常感谢您的意见!
瓦尔
In my application I have a products model which has among other things four fields for image paths. I use this to build a slide show.
However, I would love to have all those paths in one big text field and seperate them by whatever works (linebreak would be the easiest to handle in the form).
I was thinking something like:
<% for ... in @screenshots %>
<%= lightbox_to(@product.screenshot, @product.screenshot, "screenshots") %>
<% end %>
and would be hoping for that to result in:
<%= lightbox_to(@product.screenshot1, @product.screenshot1, "screenshots") %>
<%= lightbox_to(@product.screenshot2, @product.screenshot2, "screenshots") %>
<%= lightbox_to(@product.screenshot3, @product.screenshot3, "screenshots") %>
...
Your input is greatly appreciated!
Val
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想将所有链接放在一个文本字段中,则可以使用 split< /a>.
默认情况下,它将按空格分割。但你可以自己定义分裂条件。
If you want to have all links in one text field, then you can use split.
By default it will split on whitespaces. But you can define splitting condition by yourself.
假设@product 有很多屏幕截图(如果没有,请使用@screenshots 而不是下面的@product.screenshots)。
(这假设 lightbox_to 被正确调用)
如果产品确实有名为“screenshot1”、“screenshot2”等的单独成员,则执行以下操作:
Assuming @product has_many screenshots (and if not, use @screenshots instead of @product.screenshots below).
(this assumes lightbox_to is being invoked correctly)
If product really has separate members named 'screenshot1', 'screenshot2', etc., then do this: