【问题标题】:Ruby / Rails - Paperclip::Error in ModificationsController#createRuby / Rails - Paperclip::ModificationsController#create 中的错误
【发布时间】: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


    【解决方案1】:

    您在模型中声明了两次 has_attached_file :image 方法:modification.rb

    尝试删除第一个 has_attached_file :image 并告诉我这是否适合您。

    【讨论】:

    • 如果上述解决方案不起作用,请通过替换控制器中的代码行来尝试此解决方案 modifications_controller.rb: params.require(:modification).permit(:image [:image_file_name, :image_file_size, :image_content_type, :image_updated_at], :title) 你是否完成了迁移?您的数据库中有这些列吗? t.string "image_file_name" t.string "image_content_type" t.integer "image_file_size" t.datetime "image_updated_at" [github.com/thoughtbot/paperclip#quick-start]
    • 很好地理解了重复声明。我从我的代码中删除了它(包括上面问题中的摘录)。但是,出现了相同的错误消息。我通过添加迁移和架构来编辑我的问题。任何进一步的指导都会有所帮助。
    • 显然模式文件显示您在数据库的修改表中没有图像字段。您可以删除此文件:_create_modifications.rb,并将此代码添加到您的命令行:rails generate paperclip modification image,然后运行rake db:migrate,然后重新启动服务器。
    • 如@Blackcoat77 所建议的,该问题已通过删除以前的迁移之一并生成回形针的解决方案得到解决。
    猜你喜欢
    • 1970-01-01
    • 2010-11-23
    • 2015-01-07
    • 2015-07-20
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    相关资源
    最近更新 更多