6b6cdb836ea3615e42a12180f13b1b1d

利用paperclip 製作多圖片上傳的問題

2012-06-06 19:35:29 +0800a5893674Ruby on Rails 節點 中發起
最後由 chiayi2012-06-20 10:42:55 +0800回應 , 257次閱讀

我是rails 的新手
我參照 http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
裡面所教 打算把paperclip 改成可以多個檔案上傳

但我設定完成後發現
我的檔案並不會存起來
model/model_image.rb
class ModelImage < ActiveRecord::Base
belongs_to :post
has_attached_file :photo, :styles => { :s =>"100x100>",:m => "150x150>", :l => "600x600>"}, :whiny => false
end
model/post.rb
class Post < ActiveRecord::Base
has_many :model_images ,:dependent => :destroy
accepts_nested_attributes_for :model_images,:reject_if => lambda { |t| t['model_image'].nil? }
has_many :post_tag_ships
has_many :tags, :through => :post_tag_ships
belongs_to :user
attr_accessible :title, :content ,:user, :board_id, :excerpt_image, :is_general, :model_images_attributes
has_attached_file :photo, :styles => { :s =>"100x100>",:m => "150x150", :l => "600x600>"}
def posted_time
time = self.created_at
time = time.strftime("%m/%d %H:%M")
end
end

LOG:

Started POST "/posts" for 127.0.0.1 at 2012-06-06 19:21:06 +0800
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"TGTYhhNj1F7tYQMX1+QLbLHBL+zeHuSNqcizKwWu3RE=", "post"=>{"title"=>"", "content"=>"", "is_general"=>"true", "model_images_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fc554164768 @original_filename="screen-capture.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[model_images_attributes][0][image]\"; filename=\"screen-capture.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/0k/r0s6wrts1l52yj8z94_qj61c0000gn/T/RackMultipart20120606-78613-uiobxi>>}}}, "commit"=>"Create Post"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.0ms) begin transaction
SQL (0.5ms) INSERT INTO "posts" ("content", "created_at", "is_general", "keyword", "post_tag_ship_id", "score", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["content", ""], ["created_at", Wed, 06 Jun 2012 11:21:06 UTC +00:00], ["is_general", true], ["keyword", nil], ["post_tag_ship_id", nil], ["score", nil], ["title", ""], ["updated_at", Wed, 06 Jun 2012 11:21:06 UTC +00:00], ["user_id", 1]]
[paperclip] Saving attachments.
(1.0ms) commit transaction
Redirected to http://0.0.0.0:3000/posts/20
Completed 302 Found in 5ms (ActiveRecord: 1.7ms)

發現並沒有出現錯誤警告,但卻沒有存檔(也沒有轉換的動作)

p.s.我使用單一檔案上傳 不改成多檔案時,是可做的
rails 3.2.3
ruby 1.9.3p194

可以請各位幫忙看一下嘛

截至 2012-06-20 10:42:55 +0800,共收到 4 條回應
6b6cdb836ea3615e42a12180f13b1b1d
a5893674 1樓, 於2012-06-06 19:54:06 +0800回應

另外我有將 rails 101內所提示放置 config/initializers/paperclip.rb
仍無法解決這問題

6b6cdb836ea3615e42a12180f13b1b1d
a5893674 2樓, 於2012-06-06 20:03:52 +0800回應

class PostsController < ApplicationController
before_filter :authenticate_user! , :except => [ :show, :index ]
def index
@posts = Post.find_all_by_is_general(true)
end

def new
@post = Post.new
4.times {@post.model_images.build}

end

def create
@post = Post.new(params[:post])
@post.model_images_attributes = params[:post][:model_images_attributes]
@post.user = current_user
respond_to do |format|
if @post.save

format.html { redirect_to post_path(@post), notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

Ca75fbd03a327f616e84538423cc05ba
dpanaben 3樓, 於2012-06-19 16:21:42 +0800回應

rails g paperclip Model attachment1
rails g paperclip Model attachment2
rails g paperclip Model attachment3

in Model.rb
has_attached_file :attachment1, :styles => { :s =>"100x100>",:m => "150x150>", :l => "600x600>"}
has_attached_file :attachment2, :styles => { :s =>"100x100>",:m => "150x150>", :l => "600x600>"}
has_attached_file :attachment3, :styles => { :s =>"100x100>",:m => "150x150>", :l => "600x600>"}

醬子就可以上傳三個附件了
不知道會不會影響效能@@?

Fd779852eb8e519999f8b35c47777608
chiayi 4樓, 於2012-06-20 10:42:55 +0800回應

張貼程式碼請使用markdown,一堆麵條不好看。

需要 登入 後方可回應,如果你還沒有帳號按這裡 註冊