フォームに複数のファイルをアップロードしようとしています。ペーパークリップとネストされた属性のレールキャスト、およびこのチュートリアルhttp://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/に従いましたが、できないようですそれを機能させる...
ここでもスタックオーバーフローを検索し、すべてのペーパークリップとネストされた属性の投稿を調べましたが、答えが見つからないようです。すべてを正しく行っているようです...
フォームを送信すると、広告が作成され(広告アプリです)、すべて問題ないと表示されますが、画像データがデータベースに書き込まれず、ファイルもアップロードされません.. .
だから私は分類されたモデルを持っています:
class Classified < ActiveRecord::Base
has_many :classified_images, :dependent => :destroy
accepts_nested_attributes_for :classified_images, :reject_if => lambda { |t| t['classified_image'].blank? }
attr_accessible :classified_images_attributes, :access, :contact, :price, :bizType
end
次に、Classified_Image モデル:
class ClassifiedImage < ActiveRecord::Base
belongs_to :classified
has_attached_file :photo, :styles => {:small => "150x150>", :large => "320x240>"},
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/classifieds/:id/:style/:basename.:extension"
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
attr_accessible :caption, :photo
end
分類されたコントローラーの「新しい」部分には、次のものがあります。 def new @classified = Classified.new
3.times { @classified.classified_images.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @classified }
end
end
「_form」には次のものがあります。
<%= form_for @classified, :html => { :multipart => true } do |f| %>
...
<%= f.fields_for :classified_images do |builder| %>
<%= render 'image_fields', :f => builder %>
<% end %>
「image_fields」パーシャルで私は持っています:
<% if f.object.new_record? %>
<li>
<%= f.label :caption %>
<%= f.text_field :caption %>
<%= f.label :photo %>
<%= f.file_field :photo %>
</li>
<% end %>
私が持っている移行ファイルについて:
class AddAttachmentPhotoToClassifiedImages < ActiveRecord::Migration
def self.up
add_attachment :caption, :classified_id, :photo
end
def self.down
drop_attached_file :caption, :classified_id, :photo
end
end
class CreateClassifiedImages < ActiveRecord::Migration
def change
create_table :classified_images do |t|
t.string :caption
t.integer :classified_id
t.timestamps
end
end
end
「development.rb」ファイルには次のものがあります。
Paperclip.options[:command_path] = "/usr/local/bin/"
Paperclip.options[:log] = true
フォームをコミットしたときのログの例を次に示します。
127.0.0.1 の POST "/classifieds" を 2013-05-19 23:39:43 +0100 で開始 HTML パラメーターとして ClassifiedsController#create で処理: {"utf8"=>"✓", "authenticity_token"=>"978KGJSUlmMEvr6Tysg5xYIEQzNLn5vod07g+ Z7njkU=", "classified"=>{"contact"=>"918218338", "price"=>"1500", "access"=>"bons", "classified_images_attributes"=>{"0"=>{" caption"=>"テスト", "写真"=>#@original_filename="064_dont-count-the-days.jpg", @content_type="image/jpeg", >@headers="Content-Disposition: form-data ; name=\"classified[classified_images_attributes][0][photo]\"; filename=\"064_dont-count-the-days.jpg\"\r\nContent-Type:image/jpeg\r\n", >@tempfile=#3954-11t04t>>}, "1"=>{"キャプション"=>""}, "2"=>{"キャプション"=>""} }}, "commit"=>"Criar novo >Classificado"} (0.1ms) トランザクション SQL の開始 (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at", "price",) > VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22:39:43 UTC +00 :00], ["price", 1500], ["updated_at", 2013 年 5 月 19 日 (日) 22:39:43 UTC >+00:00]] (0.8ms) commit トランザクション localhost:3000/classifieds/8 にリダイレクト完了 302 Found in 5ms (ActiveRecord: 1.4ms)>}, "1"=>{"caption"=>""}, "2"=>{"caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms ) トランザクション SQL の開始 (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons "], ["contact", "918218338"], ["created_at", Sun, 19 >2013 年 5 月 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun 、2013 年 5 月 19 日 22:39:43 UTC >+00:00]] (0.8 ミリ秒) トランザクションのコミット localhost:3000/classifieds/8 にリダイレクトされました 完了 302 5 ミリ秒で見つかりました (ActiveRecord: 1.4 ミリ秒)>}, "1"=>{"caption"=>""}, "2"=>{"caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms ) トランザクション SQL の開始 (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons "], ["contact", "918218338"], ["created_at", Sun, 19 >2013 年 5 月 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun 、2013 年 5 月 19 日 22:39:43 UTC >+00:00]] (0.8 ミリ秒) トランザクションのコミット localhost:3000/classifieds/8 にリダイレクトされました 完了 302 5 ミリ秒で見つかりました (ActiveRecord: 1.4 ミリ秒)caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms) SQL トランザクションの開始 (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at" , "価格",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22 :39:43 UTC +00:00], ["price", 1500], ["updated_at", 2013 年 5 月 19 日 22:39:43 UTC >+00:00]] (0.8ms) commit トランザクションにリダイレクトlocalhost:3000/classifieds/8 完了 302 5ms で見つかりました (ActiveRecord: 1.4ms)caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms) SQL トランザクションの開始 (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at" , "価格",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22 :39:43 UTC +00:00], ["price", 1500], ["updated_at", 2013 年 5 月 19 日 22:39:43 UTC >+00:00]] (0.8ms) commit トランザクションにリダイレクトlocalhost:3000/classifieds/8 完了 302 5ms で見つかりました (ActiveRecord: 1.4ms), "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at" , Sun, 19 >May 2013 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 19 May 2013 22:39:43 UTC >+00:00]] (0.8ms) commit transaction localhost:3000/classifieds/8 にリダイレクト 完了 302 Found in 5ms (ActiveRecord: 1.4ms), "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at" , Sun, 19 >May 2013 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 19 May 2013 22:39:43 UTC >+00:00]] (0.8ms) commit transaction localhost:3000/classifieds/8 にリダイレクト 完了 302 Found in 5ms (ActiveRecord: 1.4ms)8ms) トランザクションのコミット localhost:3000/classifieds/8 にリダイレクト 完了 302 5ms で発見 (ActiveRecord: 1.4ms)8ms) トランザクションのコミット localhost:3000/classifieds/8 にリダイレクト 完了 302 5ms で発見 (ActiveRecord: 1.4ms)
ご覧のとおり、「classifieds」テーブルには挿入されますが、「classifieds_image」テーブルには挿入されません。また、ペーパークリップから情報を取得できません...
すべてのコードで申し訳ありませんが、これは私が見ていない本当に単純なものである必要があり、より多くの情報が得られるので、より良い助けになるでしょう...さらにコードや情報が必要な場合はお知らせください.. .