【发布时间】:2017-02-10 19:42:36
【问题描述】:
Paperclip::ModificationsController 中的错误#create Modification model missing required attr_accessor for 'image_file_name'
错误:
型号: modify.rb
class Modification < ActiveRecord::Base
has_attached_file :image,
styles: { thumb: ["64x64#", :jpg],
original: ['500x500>', :jpg] },
convert_options: { thumb: "-quality 75 -strip",
original: "-quality 85 -strip" }
validates_attachment :image,
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
end
控制器: modify_controller.rb
class ModificationsController < ApplicationController
def index
@modifications = Modification.order('created_at')
end
def new
@modifications = Modification.new
end
def create
@modifications = Modification.new(modification_params)
if @modifications.save
flash[:success] = "Modification contributed!"
redirect_to collection_path
else
render 'new'
end
end
private
def modification_params
params.require(:modification).permit(:image, :title)
end
end
迁移: _create_modifications.rb
class CreateModifications < ActiveRecord::Migration
def change
create_table :modifications do |t|
t.string :title
t.string :image_file_name
t.string :image_content_type
t.integer :image_file_size
t.timestamps null: false
end
end
end
迁移: _add_attachment_modification_to_profiles.rb
class AddAttachmentModificationToProfiles < ActiveRecord::Migration
def self.up
change_table :profiles do |t|
t.attachment :modification
end
end
def self.down
remove_attachment :profiles, :modification
end
end
Schema.rb
create_table "modifications", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
...
create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.string "first_name"
t.string "last_name"
t.string "location"
t.string "modifications"
t.string "website"
t.text "bio"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.string "modification_file_name"
t.string "modification_content_type"
t.integer "modification_file_size"
t.datetime "modification_updated_at"
end
【问题讨论】:
标签: ruby-on-rails model-view-controller model controller paperclip