轨道3与Carrierwave和简单的表单多态关联 [英] Rails 3 polymorphic association with Carrierwave and Simple Form

查看:213
本文介绍了轨道3与Carrierwave和简单的表单多态关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设立其使用Carrierwave处理图片上传的多态关联。我用简单的形式建立我的方式。我觉得联想是正确的,所以我想知道如果我的问题只是一些与窗体或控制器。

I'm trying to set up a polymorphic association for photo uploads which are processed using Carrierwave. I'm using Simple Form to build my forms. I feel like the association is correct so I'm wondering if my problem is just something with the form or controller.

下面是我的协会:

property.rb:

property.rb:

class Property < ActiveRecord::Base
  attr_accessible :image
  ...
  has_many :image, :as => :attachable
  ...
end

unit.rb

unit.rb

class Unit < ActiveRecord::Base
  attr_accessible :image
  ...
  has_many :image, :as => :attachable
end

image.rb

image.rb

class Image < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true
  mount_uploader :image, PhotoUploader
end

properties_controller.rb:

properties_controller.rb:

def edit
    @property = Property.find params[:id]
    @property.image.build if @property.image.empty?
end

def update
    @property = Property.find params[:id]
    if @property.update_attributes params[:property]
        redirect_to admin_properties_path, :notice => 'The property has been successfully updated.'
    else
        render "edit"
    end
end

片段从性能/ _form.html.erb

Snippet from properties/_form.html.erb

<%= f.input :image, :label => 'Image:', :as => :file %>

下面是我附有图片提交时出现错误:

Here is the error I get when submitting with an image attached:

undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x00000102291bb8>

和这里的PARAMS:

And here are the params:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"lvB7EMdc7juip3gBZD3XhCLyiv1Vwq/hIFdb6f1MtIA=",
 "property"=>{"name"=>"Delaware Woods",
 "address"=>"",
 "city"=>"",
 "state"=>"",
 "postal_code"=>"",
 "description"=>"2 bedroom with large kitchen.  Garage available",
 "incentives"=>"",
 "active"=>"1",
 "feature_ids"=>[""],
 "user_ids"=>[""],
 "image"=>#<ActionDispatch::Http::UploadedFile:0x00000102291bb8 @original_filename="wallpaper-4331.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"property[image]\"; filename=\"wallpaper-4331.jpg\"\r\nContent-Type: image/jpeg\r\n",
 @tempfile=#<File:/tmp/RackMultipart20120608-3102-13f3pyv>>},
 "commit"=>"Update Property",
 "id"=>"18"}

我到处找对多态关联的帮助,我一事无成。我见过,看起来pretty的直接的简单的例子。有一件事我注意到的是,它看起来像很多在我的情况下的has_many协会应图片,而不是图片<例子/ code>。然而,当我这样做,我得到一个错误:

I'm looking everywhere for help on polymorphic associations and am getting nowhere. I've seen simple examples that look pretty straight forward. One thing I've noticed is that it seems like in a lot of the examples the has_many association in my case should be images and not image. However when I do that I get an error:

Can't mass-assign protected attributes: image

我试着更新我的形式使用fields_for,因为我已经看到了在其他的博客像这样:

I've tried updating my form to use fields_for as I've seen in other blogs like so:

<%= f.input :image, :label => "Photo", :as => :file %>

<% f.simple_fields_for :images do |images_form| %>
        <%= images_form.input :id, :as => :hidden %>
        <%= images_form.input :attachable_id, :as => :hidden %>
        <%= images_form.input :attachable_type, :as => :hidden %>
        <%= images_form.input :image, :as => :file %>
<% end %>

我只知道我有一个时间赫克得到这个工作。我是pretty的新到Rails这样即使调试也很难。它并不能帮助调试器并没有真正的工作在3.2:(

All I know is I'm having a heck of a time getting this to work. I'm pretty new to Rails so even debugging it is difficult. It doesn't help that the debugger doesn't really work in 3.2 :(

推荐答案

由于您的模型have_many:图像(它应该是:图像,而不是:图像),你会想用nested_forms在你的看法。您应该建立accepts_nested_attributes_for:图片上的单元和属性模型,并从改变attr_accessible:图片:image_attributes

Since your models have_many :images (it should be :images, not :image), you'll want to use nested_forms in your views. You should set up accepts_nested_attributes_for :images on the unit and property models and change the attr_accessible from :image to :image_attributes.

查看 http://railscasts.com/episodes/196-嵌套模型的形式部分-1 有关获取它会很好的指导作用。

Check out http://railscasts.com/episodes/196-nested-model-form-part-1 for a good guide on getting going with it.

这篇关于轨道3与Carrierwave和简单的表单多态关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆