Rails 4 Paperclip没有使用STI将图像保存到db

时间:2014-09-13 11:44:36

标签: ruby-on-rails-4 paperclip sti

我无法使用paperclip将图像保存到数据库,我使用了不同的回形针版本(3.5.0,4.2和git repo)。目前我按照Gem的文档推荐使用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; NAME = “驱动器[化身]”; filename =“darth vader.jpeg”   内容类型:image / jpeg

然而,图像没有保存到db,任何想法?

我知道paperclip正在项目中工作,因为我在另一个不使用STI的模型上。

1 个答案:

答案 0 :(得分:0)

问题出现在允许的参数中,它应该是指导者而不是驾驶员,如下所示:

def permitted_params
  params.require(:instructor).permit(..., :avatar)
end