我发布新派对时收到以下错误消息:派对模型缺少attr_accessor需要&#39; image_file_name&#39; ,它指向我的parties_controller.rb中的代码:< BR />
def create
@party = Party.new(party_params)
我很难理解这个错误的原因,
这是我正在使用的其他代码
party.rb
class Party < ActiveRecord::Base
belongs_to :user
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates :description, presence: true
validates :image, presence: true
end
parties_controller.rb
class PartiesController < ApplicationController
before_action :set_party, only: [:show, :edit, :update, :destroy]
# GET /parties
# GET /parties.json
def index
@parties = Party.all
end
# GET /parties/1
# GET /parties/1.json
def show
end
# GET /parties/new
def new
@party = Party.new
end
# GET /parties/1/edit
def edit
end
# POST /parties
# POST /parties.json
def create
@party = Party.new(party_params)
respond_to do |format|
if @party.save
format.html { redirect_to @party, notice: 'Party was successfully created.' }
format.json { render :show, status: :created, location: @party }
else
format.html { render :new }
format.json { render json: @party.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /parties/1
# PATCH/PUT /parties/1.json
def update
respond_to do |format|
if @party.update(party_params)
format.html { redirect_to @party, notice: 'Party was successfully updated.' }
format.json { render :show, status: :ok, location: @party }
else
format.html { render :edit }
format.json { render json: @party.errors, status: :unprocessable_entity }
end
end
end
# DELETE /parties/1
# DELETE /parties/1.json
def destroy
@party.destroy
respond_to do |format|
format.html { redirect_to parties_url, notice: 'Party was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_party
@party = Party.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def party_params
params.require(:party).permit(:latitude, :longitude, :address, :description, :title, :image)
end
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, #:confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_many :parties
validates :name, presence: true
#validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+edu)\z/ }
end
答案 0 :(得分:1)
发生此错误的原因是在db / migrations文件中创建了重复的迁移文件。您需要做的是删除所有这些文件,然后在终端上执行rake db:rollback以取消所有迁移。然后做一个rails d paperclip MODEL(用模型u r替换名称)图像。 然后做一个rails g paperclip MODEL图像然后rake db:migrate。它现在应该工作
答案 1 :(得分:0)
首先,您的image_file_name
不应该是attr_accessor
- 它应该是您party
对象的实际属性(@party.image_file_name
)
考虑到您已使用Paperclip执行了所需的migrations
设置db
,我相信问题可能出在以下问题:
validates :image, presence: true
我相信你会更好地使用内置的Paperclip AttachmentPresenceValidator
,如下所示:
class Party < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_presence :image
end
原因是当您将image
对象传递给Party
模型时,您不会发送“裸”对象。 Paperclip在通过之前为对象分配了各种属性,这就是为什么你看到attr_accessor
错误(Paperclip在保存之前分配属性,因此它必须使用attr_accessor
来创建它们)
使用内置的validates_attachment
方法,您可以直接验证Paperclip
对象