Steven L
Steven L

Reputation: 131

ActiveRecord::UnknownAttributeError (unknown attribute: user_id):

My app on Heroku shows this error: We're sorry, but something went wrong. If you are the application owner check the logs for more information.

So I ran Heroku logs and guessed this could be the problem:

ActiveRecord::UnknownAttributeError (unknown attribute: user_id):
app/controllers/pins_controller.rb:14:in `new'

My Pins controller

class PinsController < ApplicationController
  before_action :set_pin, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]



def index
    @pins = Pin.all
  end

  def show
  end

  def new
    @pin = current_user.pins.build
  end

  def edit
  end

  def create
    @pin = current_user.pins.build(pin_params)
    if @pin.save
      redirect_to @pin, notice: 'Pin was successfully created.'
    else
      render action: 'new'
    end
  end



def update
    if @pin.update(pin_params)
      redirect_to @pin, notice: 'Pin was successfully updated.'
    else
      render action: 'edit'
    end
  end

  def destroy
    @pin.destroy
    redirect_to pins_url
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_pin
      @pin = Pin.find(params[:id])
    end

    def correct_user
      @pin = current_user.pins.find_by(id: params[:id])
      redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil? 
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pin_params
      params.require(:pin).permit(:description, :image)
    end
end

Anything wrong? Am I looking at the right place to debug?

Turns out I didn't do heroku run rake db:migrate. Thanks guys for that. Another error came up.

ArgumentError (missing required :bucket option):
app/controllers/pins_controller.rb:22:in `create'

Is this tied to Amazon Web Services?

Upvotes: 0

Views: 3268

Answers (2)

Joshua Dance
Joshua Dance

Reputation: 10482

You combined 2 issues into one question. For the first problem with the unknown attribute user_id you need to run:

heroku run rake db:migrate.

For the second problem with your ArgumentError (missing required :bucket option): error you need to set the heroku config to:

heroku config:set S3_BUCKET_NAME=nameOfYourBucket

Upvotes: 1

Ruby Racer
Ruby Racer

Reputation: 5740

I'd go in New action for plain

@pin = Pin.new

You are using your Create action to add the values from relations, so it should work fine

Other then that, use .new in New and .build in Create

Upvotes: 0

Related Questions