Rails - 迭代一个字段中的数据库条目,以换行符分隔

发布于 2024-08-17 13:29:58 字数 634 浏览 1 评论 0原文

在我的应用程序中,我有一个产品模型,其中包含四个图像路径字段。我用它来制作幻灯片。

然而,我希望将所有这些路径放在一个大文本字段中,并通过任何有效的方式将它们分开(换行符将是表单中最容易处理的)。

我在想这样的事情:

<% 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 技术交流群。

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

发布评论

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

评论(2

趁微风不噪 2024-08-24 13:29:58

如果您想将所有链接放在一个文本字段中,则可以使用 split< /a>.

<% @product.screenshots.split.each do |screenshot| %>
  <%= lightbox_to(screenshot, screenshot, "screenshots" %>
<% end %>

默认情况下,它将按空格分割。但你可以自己定义分裂条件。

If you want to have all links in one text field, then you can use split.

<% @product.screenshots.split.each do |screenshot| %>
  <%= lightbox_to(screenshot, screenshot, "screenshots" %>
<% end %>

By default it will split on whitespaces. But you can define splitting condition by yourself.

南渊 2024-08-24 13:29:58

假设@product 有很多屏幕截图(如果没有,请使用@screenshots 而不是下面的@product.screenshots)。

<% @product.screenshots.each do |screenshot| %>
   <%= lightbox_to(screenshot, screenshot, "screenshots") %>
<% end %>

(这假设 lightbox_to 被正确调用)

如果产品确实有名为“screenshot1”、“screenshot2”等的单独成员,则执行以下操作:

<% [:screenshot1, :screenshot2, :screenshot3].each do |screenshot_name|
   screenshot = @product.send screenshot_name %>
  <%= lightbox_to(screenshot, screenshot, "screenshots") %>
<% end %>`

Assuming @product has_many screenshots (and if not, use @screenshots instead of @product.screenshots below).

<% @product.screenshots.each do |screenshot| %>
   <%= lightbox_to(screenshot, screenshot, "screenshots") %>
<% end %>

(this assumes lightbox_to is being invoked correctly)

If product really has separate members named 'screenshot1', 'screenshot2', etc., then do this:

<% [:screenshot1, :screenshot2, :screenshot3].each do |screenshot_name|
   screenshot = @product.send screenshot_name %>
  <%= lightbox_to(screenshot, screenshot, "screenshots") %>
<% end %>`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文