ActiveRecord :: UnknownAttributeError(未知属性:user_id):

时间:2014-02-22 17:07:56

标签: ruby-on-rails activerecord heroku

我在Heroku上的应用程序显示此错误: 我们很抱歉,但有些不对劲。 如果您是应用程序所有者,请检查日志以获取更多信息。

所以我运行了Heroku日志并猜测这可能是问题所在:

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

My Pins控制器

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

有什么不对吗?我在寻找合适的地方进行调试吗?

原来我没有做heroku run rake db:migrate。谢谢大家。出现了另一个错误。

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

这与亚马逊网络服务有关吗?

2 个答案:

答案 0 :(得分:1)

您将2个问题合并为一个问题。对于未知属性user_id的第一个问题,您需要运行:

heroku run rake db:migrate

对于ArgumentError (missing required :bucket option):错误的第二个问题,您需要将heroku配置设置为:

heroku config:set S3_BUCKET_NAME=nameOfYourBucket

答案 1 :(得分:0)

我会参加普通的新动作

@pin = Pin.new

您正在使用“创建”操作添加关系中的值,因此它应该可以正常工作

除此之外,在Create和.build中使用.new创建