ActiveRecord::UnknownAttributeError
Ich versuche zu schaffen-hotel mit einigen Feldern, eines der Felder ist Foto, ich möchte mehrere Dateien hochladen mit carrierwave und nested_form. Ich fand diese Artikel
und haben einige Ergebnis.
Wenn ich auf /hotels/neu, ausfüllen von Feldern, Auswahl von Fotos
und drücken Sie senden, immer ActiveRecord::UnknownAttributeError in HotelsController#unbekanntes Attribut: attachable_type.
Konsole
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'
...
- Modell-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
Modell-Anlage.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 %>
Irgendwelche Ideen, Jungs? Was mache ich hier falsch?
InformationsquelleAutor yozzz | 2013-09-27
Du musst angemeldet sein, um einen Kommentar abzugeben.
ActiveRecord::UnknownAttributeError bedeutet, dass Ihnen fehlt ein Feld in der Datenbank. Sieht aus wie Sie verpasste ein Feld, wenn Sie die Einrichtung Ihres polymorphe Beziehung, oder Sie vergessen haben, führen Sie Ihre Migrationen.
Siehe auch: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
InformationsquelleAutor Taavo
versuchen, erstellen Sie eine Datei mit Namen
app/Uploader/photo_uploader.rb
dann in Ihrem Modell hinzufügen
haben Sie wahrscheinlich diese
/config/Initialisierungen/carrierwave.rb
Was ist carrierwave.rb-Datei, was tut er?
/config/Initialisierungen/carrierwave.rb es verbindet die AWS um die Dateien zu speichern, können Sie Sie ersetzen
ENV['CARRIER_WAVE_ACCESS_KEY']
mit Ihren Werten zu verbindenInformationsquelleAutor MZaragoza