【发布时间】:2014-09-24 05:43:34
【问题描述】:
我无法使用回形针将图像保存到数据库,我使用了不同的回形针版本(3.5.0、4.2 和来自 git repo)。目前我正在使用 Gem's Docs 中推荐的 4.2。
class Instructor < ActiveRecord::Base
#also tried having the has_attached method in this class
end
class Driver < Instructor
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
schema.rb
create_table "instructors", force: true do |t|
....
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end
现在在我的驱动程序/编辑页面中:
<%= simple_form_for (@driver), id: "step_form", :html => { :multipart => true }, :class => "form-group", url: wizard_path do |f| %>
...
<div class="form-group">
<label>Upload an Image</label>
<div class="span7"><%= f.file_field :avatar %>
</div>
</div>
<div class="actions">
<button type="submit" class="btn btn-success">Next</button>
or <%= link_to "skip this step", next_wizard_path %>
</div><br>
<%end %>
在我的驱动程序控制器中
class DriversController < InstructorController
...
def update
@driver = current_instructor
if @driver.update(permitted_params)
flash[:success] = "Profile updated"
redirect_to :dashboard
else
flash[:error] = "Profile not updated"
render :edit
end
end
private
def permitted_params
params.require(:driver).permit(..., :avatar)
end
end
以及来自浏览器的发布操作的有效负载
... ------WebKitFormBoundaryIV0A8q3XIQ4pTWFr Content-Disposition: form-data;名称="司机[头像]";文件名="darth vader.jpeg" 内容类型:image/jpeg
但是图像没有保存到数据库中,有人知道吗?
我知道回形针正在该项目中工作,因为我在另一个不使用 STI 的模型上使用了它。
【问题讨论】:
标签: ruby-on-rails-4 paperclip sti