Model missing required attr_accessor for 'imag

2019-06-01 11:39发布

I receive the following error message when i post a new party: Party model missing required attr_accessor for 'image_file_name' and it points to the code in my parties_controller.rb:

def create
    @party = Party.new(party_params)

Im struggling to understand the reason for this error,

Here is the other code i'm working with

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

2条回答
混吃等死
2楼-- · 2019-06-01 12:28

This error occurs because there are duplicate migration files created in the db/migrations file. What you need to do is delete all these files then on your terminal do a rake db:rollback to cancel all migrations. then do a rails d paperclip MODEL(replace name with model u r using) image. Then do a rails g paperclip MODEL image then rake db:migrate. it should work now

查看更多
祖国的老花朵
3楼-- · 2019-06-01 12:28

Firstly, your image_file_name should not be an attr_accessor - it should be an actual attribute of your party object (@party.image_file_name)

Considering you have performed the required migrations to set up your db with Paperclip, I believe the problem will likely be with this line:

validates :image, presence: true

I believe you'll be better using the inbuilt Paperclip AttachmentPresenceValidator like so:

   class Party < ActiveRecord::Base
        has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
        validates_attachment_presence :image
    end

The reason for this is that when you pass the image object to your Party model, you're not sending the "naked" object. Paperclip assigns various attributes to the object before it passes, which is why you're seeing the attr_accessor error (Paperclip assigns the attributes before it saves, so it has to use attr_accessor to create them)

Using the inbuilt validates_attachment methods will give you the ability to validate the Paperclip object directly

查看更多
登录 后发表回答