使用带有 Mongoid 和 Carrierwave 的 Rails3 以嵌套形式上传文件的问题
我在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己也遇到了类似的问题。我认为问题不是图像上传,而是 Rails 无法将 :images 识别为数组。如果您查看 fields_for 帮助器的 Rails 源代码,您会发现它检查方法“_attributes=”。如果不存在,表单将作为普通字段而不是数组发布(参数将是“images”而不是“images[0]”)
您必须将以下行添加到模型中:
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:
这是 Carrierwave 或 mongoid bug
https://github.com/jnicklas/rierwave/issues#issue /81
It is carrierwave or mongoid bug
https://github.com/jnicklas/carrierwave/issues#issue/81
这很可能是 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.