0
我无法使用回形针将图像保存到数据库,我使用了不同的回形针版本(3.5.0,4.2和git回购)。目前我使用的是Gem's Docs中推荐的4.2。Rails 4回形针没有将图像保存到带STI的db
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; NAME = “驱动器[化身]”;文件名=“达斯·vader.jpeg” 内容类型:图像/ JPEG
但是图像不会被保存到数据库,想法吗?
我知道paperclip正在项目中工作,因为我在不使用STI的另一个模型上使用它。