2013-09-27 30 views
4

我想創建一些領域的酒店,其中一個領域是照片,我想使用carrierwave和nested_form上載多個文件。我發現這個article 並且有一些結果。ActiveRecord :: UnknownAttributeError

,當我在/hotels/new,灌裝領域,選擇照片 並按提交,得到的ActiveRecord :: UnknownAttributeError在HotelsController#創建未知屬性: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 %> 

任何想法的傢伙?我在這裏做錯了什麼?

回答

0

嘗試創建一個文件名 應用程序/上傳/ photo_uploader.rb

# encoding: utf-8 
class PhotoUploader < CarrierWave::Uploader::Base 
    # Include RMagick or MiniMagick support: 
    include CarrierWave::RMagick 
    # include CarrierWave::MiniMagick 
    include Sprockets::Helpers::RailsHelper 
    include Sprockets::Helpers::IsolatedHelper 
    include CarrierWave::MimeTypes 
    process :set_content_type 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
    def extension_white_list 
    %w(jpg jpeg gif png) 
    end 
# process :resize_to_fit => [800, 800] 

    version :thumb do 
    process :resize_to_limit => [50, 50] 
    end 
end 

然後在模型中添加

mount_uploader :image, PhotoUploader 
mount_uploader :image2, PhotoUploader 

你可能有這樣的

/配置/initializers/carrierwave.rb

CarrierWave.configure do |config| 
    config.storage = :fog 
    config.fog_credentials = { 
    :provider => 'AWS', 
    :aws_access_key_id => ENV['CARRIER_WAVE_ACCESS_KEY'], 
    :aws_secret_access_key => ENV['CARRIER_WAVE_SECRET_ACCESS_KEY'] 
    } 
    config.cache_dir = "#{Rails.root}/tmp/uploads" 
    config.fog_directory = ENV['CARRIER_WAVE_BUCKET'] 
    config.fog_attributes = { 'Cache-Control'=>'max-age=315576000' } 
    #config.fog_public = false # optional, defaults to true 
end 
+0

遺憾的是,仍然不能正常工作,得到了同樣的錯誤 – yozzz

+0

什麼是carrierwave.rb文件,他做什麼呢? – yozzz

+0

/config/initializers/carrierwave.rb它連接到AWS以存儲文件,您可以用您的值替換'ENV ['CARRIER_WAVE_ACCESS_KEY']'連接 – MZaragoza

相關問題