使用带有 Mongoid 和 Carrierwave 的 Rails3 以嵌套形式上传文件的问题

发布于 2024-09-10 21:13:16 字数 978 浏览 4 评论 0原文

我在将 SQLlite Rails 3 应用程序传输到 Mongoid Rails 3 应用程序时遇到问题。在 SQLlite 版本中,我可以轻松地将一个模型(“图像”)的图像上传表单(使用 Paperclip)包含在另一个模型(“产品”)的嵌套表单中。这是我的“新”产品表单:

  <%= form_for @product, :html => {:multipart => true} do |f| %>
     <% f.fields_for :images do |image_form| %>
       <%= f.label :productphoto %>
       <%= f.file_field :productphoto %><br />
    <% end %>
 <% end %>

这是“显示”视图:

    <% @product.images.each do |image| %>
      <%= image_tag image.productphoto.url(:gallerythumb) %><br />
    <% end %>

当我尝试在我的 Mongoid Rails 3 应用程序(使用 Carrierwave)中使用相同的产品视图时,我收到以下错误:

    TypeError in Stores#show: 
    can't convert nil into String
    <%= image_tag product.image.url(:gallerythumb) %>

我很确定我的模型在 Mongoid 中版本是正确的,因为如果我向“图像”模型添加一个字符串(如“名称”)并将其嵌套在“产品”表单中,它就可以工作。另外,我能够将图像上传到非嵌套模型表单中。

任何帮助将不胜感激!

Im having a problem transferring an SQLlite Rails 3 app over to a Mongoid Rails 3 app. In the SQLlite version, I am easily able to include an image upload form (using Paperclip) from one model ('image') within a nested form from another model ('product'). Here's my 'new' product form:

  <%= form_for @product, :html => {:multipart => true} do |f| %>
     <% f.fields_for :images do |image_form| %>
       <%= f.label :productphoto %>
       <%= f.file_field :productphoto %><br />
    <% end %>
 <% end %>

And here's the 'show' view:

    <% @product.images.each do |image| %>
      <%= image_tag image.productphoto.url(:gallerythumb) %><br />
    <% end %>

When I try to use the same product views in my Mongoid Rails 3 app (using Carrierwave), I get the following error:

    TypeError in Stores#show: 
    can't convert nil into String
    <%= image_tag product.image.url(:gallerythumb) %>

Im pretty sure my models in the Mongoid version are correct because if I add a string (like 'name') to my 'image' model and nest that in the 'Product' form, it works. Also, Im able to upload an image into a non-nested model form.

Any help would be greatly appreciated!

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

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

发布评论

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

评论(3

大海や 2024-09-17 21:13:16

我自己也遇到了类似的问题。我认为问题不是图像上传,而是 Rails 无法将 :images 识别为数组。如果您查看 fields_for 帮助器的 Rails 源代码,您会发现它检查方法“_attributes=”。如果不存在,表单将作为普通字段而不是数组发布(参数将是“images”而不是“images[0]”)

您必须将以下行添加到模型中:

accepts_nested_attributes_for :images

I just had a similar problem myself. The problem is not the image upload I think, but the problem is that Rails doesn't recognize :images as being an Array. If you look into the Rails source of the fields_for helper you see that it checks for a method "_attributes=". If that's not there the form will be posted as normal fields and not as an array (params will be "images" instead of "images[0]")

You have to add the following line to your model:

accepts_nested_attributes_for :images
泼猴你往哪里跑 2024-09-17 21:13:16

这很可能是 Lewy 链接到的问题 - 该问题特定于您的 Carrierwave 上传器安装在嵌入式关联中的子文档上并且您正在保存父文档的安排,尽管您没有明确显示这是否是您的数据是如何建模的,我怀疑是这种情况,因为您注意到它适用于非嵌套表单(大概保存子文档,而不是父文档)。

如果您深入研究该问题链接的讨论,您会发现一些建议的解决方法。以下是我最终让 Carrierwave 在这种情况下为我工作的结果:

https://gist.github.com/759788

完全归功于 Zerobearing2,我分叉了其要点,我只是做了一些小的更改以使其在 Rails 3.0.3 中工作,并通过相关讨论的摘要信息对我的要点进行了评论。

This is most likely the issue that Lewy linked to -- that problem is specific to arrangements where your Carrierwave uploader is mounted on a child document in an embedded association and you are saving the parent, and though you don't explicitly show if this is how your data is modeled, I suspect that's the case since you noted that it works with a non-nested form (presumably saving the child document then, not the parent).

If you dig around in the discussions linked from that issue, you'll find some proposed workarounds. Here's what I ended up with to get Carrierwave working in this situation for me:

https://gist.github.com/759788

Full credit is to due to zerobearing2 whose gist I forked, I just made minor changes to get it working in Rails 3.0.3 and commented on my gist with summary info on the relevant discussions.

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