【发布时间】:2013-12-09 19:02:01
【问题描述】:
我整晚都在做这件事,但没有任何意义。我正在调整一个旧的照片网络应用程序以在其中包含相册。我将“失败”(基本上是图像)作为专辑的嵌套资源。我正在使用 carrierwave 将文件上传到 S3 存储桶。
奇怪的是:对于专辑模型(专辑图片),上传工作得非常好,但对于失败模型却不能上传。
我不明白为什么它现在是嵌套资源会成为问题。显示它不是问题,因为某种原因,它通过表单很好,通过验证很好,没有抛出错误,它像成功一样重定向到失败#index,但在 db 或 S3 中没有任何内容。
代码如下。所有代码https://github.com/spq24/failboard
失败模型
class Fail < ActiveRecord::Base
attr_accessible :description, :image, :remote_image_url, :fail_title, :tag_list, :processed, :youtube_url, :album_id
make_voteable
acts_as_taggable
belongs_to :album
mount_uploader :image, ImageUploader
validates :description, length: { :maximum => 200 }
validates :album_id, presence: true
validates :image, presence: true
validates :fail_title, presence: true, length: { :maximum => 50 }
validate :maximum_amount_of_tags
def maximum_amount_of_tags
number_of_tags = tag_list_cache_on("tags").uniq.length
errors.add(:base, "Please only add up to 5 tags") if number_of_tags > 5
end
before_save :update_attachment_attributes
def update_attachment_attributes
if image.present? && image_changed?
self.content_type = image.file.content_type
self.file_size = image.file.size
end
end
def next
user.fails.where("id > ?", id).order("id ASC").first
end
def prev
user.fails.where("id < ?", id).order("id DESC").first
end
end
专辑模型
class Album < ActiveRecord::Base
attr_accessible :name, :image, :image_url, :created_at
belongs_to :user
has_many :fails, dependent: :destroy
mount_uploader :image, ImageUploader
validates :user_id, presence: true
validates :image, presence: true
validates :name, presence: true, length: { :maximum => 50 }
before_save :update_attachment_attributes
def update_attachment_attributes
if image.present? && image_changed?
#self.content_type = image.file.content_type
#self.file_size = image.file.size
end
end
def next
user.fails.where("id > ?", id).order("id ASC").first
end
def prev
user.fails.where("id < ?", id).order("id DESC").first
end
end
控制器失败
def new
@fail = Fail.new(:album_id => params[:album_id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: @fail }
end
end
def create
@fail = Fail.new(params[:fail])
respond_to do |format|
if @fail.save
format.html { redirect_to @fail.album, notice: 'You added a new photo!' }
format.json { render json: @fail, status: :created, location: @fail }
else
format.html { render action: "new" }
format.json { render json: @fail.errors, status: :unprocessable_entity }
end
end
end
routes.rb
resources :albums do
get 'tags/:tag', to: 'fails#index', as: :tag
resources :fails do
member do
post :up_vote
end
end
调试哈希(当我尝试上传时它会变成红色,但我没有看到任何会导致错误的东西)
这里是调试信息:
{"utf8"=>"✓", "authenticity_token"=>"Hz6Gl95ultYDNIEjQioIckB8JXQwhiMxXIM9jrfqd5Q=", "fail"=>{"fail_title"=>"tester", "image"=>#<ActionDispatch::Http::UploadedFile:0x56195e8 @original_filename="pic19.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"fail[image]\"; filename=\"pic19.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/Kinertia/AppData/Local/Temp/RackMultipart20131125-10428-m2ktp2>>, "description"=>"", "tag_list"=>"test"}, "commit"=>"Create Fail", "controller"=>"fails", "action"=>"index"}
如果还有什么需要请告诉我,我会放在这里。谢谢大家的帮助!
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 file-upload amazon-s3 carrierwave