微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – ActiveRecord :: UnknownAttributeError

我正在尝试用一些字段创建酒店,其中一个字段是照片,我想使用带有carrierwave和nested_form的多个文件上传.我发现这个 article
并有一些结果.

当我在/hotels/new,填写字段,选择照片
并按提交,在HotelsController#中获取ActiveRecord :: UnkNownAttributeError创建未知属性:attachable_type.
安慰

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' 
...

型号hotel.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

型号attachment.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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐