ruby-on-rails - ActiveRecord::未知属性错误

标签 ruby-on-rails activerecord carrierwave nested-forms

我正在尝试创建带有一些字段的酒店,其中一个字段是照片,我想使用带有 Carrierwave 和nested_form 的多个文件上传。我找到了这个article 并取得一些成果。

当我使用/hotels/new时、填写字段、选择照片 然后按提交,在 HotelsController#createunknown attribute: Attachable_type 中获取 ActiveRecord::UnknownAttributeError。 控制台

Started POST "/hotels" for 127.0.0.1 at 2013-09-27 17:35:18 +0300
Processing by HotelsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+1T2tuygSnj8unOKkXkRWI4L7KvDE
9PPHrqvag7KmIQ=", "hotel"=>{"title"=>"dsa", "address"=>"asd", "star_rating"=>"2"
, "breakfast"=>"Not include", "price_for_room"=>"sadas", "room_description"=>"Gr
eat room", "attachments_attributes"=>{"1380289954031"=>{"file"=>#<ActionDispatch
::Http::UploadedFile:0xa5d546c @original_filename="11374.jpg", @content_type="im
age/jpeg", @headers="Content-Disposition: form-data; name=\"hotel[attachments_at
tributes][1380289954031][file]\"; filename=\"11374.jpg\"\r\nContent-Type: image/
jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20130927-7077-50zkol>>, "_destroy"
=>"false"}, "1380289972216"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0xa5d
53a4 @original_filename="357175.jpg", @content_type="image/jpeg", @headers="Cont
ent-Disposition: form-data; name=\"hotel[attachments_attributes][1380289972216][
file]\"; filename=\"357175.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<F
ile:/tmp/RackMultipart20130927-7077-dlkmwk>>, "_destroy"=>"false"}}}, "commit"=>
"Done"}
Completed 500 Internal Server Error in 109ms

ActiveRecord::UnknownAttributeError (unknown attribute: attachable_type):
  app/controllers/hotels_controller.rb:15:in `new'
  app/controllers/hotels_controller.rb:15:in `create' 
...

模型酒店.rb

class Hotel < ActiveRecord::Base
  attr_accessible :address, :breakfast, :price_for_room, :room_description,
                :star_rating, :title, :attachments_attributes

  has_many :attachments, :as => :attachable
  accepts_nested_attributes_for :attachments
end

模型附件.rb

class Attachment < ActiveRecord::Base
  attr_accessible :file  
  belongs_to :attachable, :polymorphic => true
  mount_uploader :file, FileUploader
end

hotels_controller.rb

...
      def new
        @hotel = Hotel.new
      end
      def create
        @hotel = Hotel.new(params[:hotel])
        if @hotel.save
          redirect_to hotels_path, notice: "Nice, you added new hotel " + @hotel.title
        else
          render "new"
        end  
      end

_form.rb

<%= nested_form_for @hotel, :html => {:multipart => true}  do |f| %>
  ...
  <%= f.fields_for :attachments do |attachment_form|%>    
    <%= attachment_form.label :file %>
    <%= attachment_form.file_field :file %>
    <%= attachment_form.link_to_remove "Remove this photo" %>
  <% end %>
  <%= f.link_to_add "Add photo", :attachments %>
  <%= f.submit 'Done', class: 'btn btn-success' %>
<% end %>

大家有什么想法吗?我在这里做错了什么?

最佳答案

ActiveRecord::UnknownAttributeError 意味着您缺少数据库中的字段。看起来您在设置多态关系时错过了一个字段,或者忘记运行迁移。

另请参阅:http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

关于ruby-on-rails - ActiveRecord::未知属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19053734/

相关文章:

ruby-on-rails - 防止损坏的图像在开发中击中 Rails

mysql - :first, :order in Rails 3? 的正确查找语法

ruby-on-rails - 错误:推送到 Heroku 时未初始化常量 Sprockets::Helpers

ruby-on-rails - Carrierwave 多张图片上传

ruby-on-rails - CarrierWave - PDF - 只选择第一页

mysql - 使用 LDAP/AD 登录身份验证时,如果使用 AD 记录不存在用户行,如何创建用户行

ruby-on-rails - Rails has_one 和belongs_to 迁移?

ruby-on-rails - 如何根据多对多关系选择用户子集?

ruby-on-rails - ActiveRecord::StatementInvalid (PG::UndefinedFunction

activerecord - Yii2:如何在 ActiveRecord 中设置默认属性值?