【问题标题】:ActiveRecord::UnknownAttributeError (unknown attribute: user_id):ActiveRecord::UnknownAttributeError(未知属性:user_id):
【发布时间】:2014-02-22 17:07:56
【问题描述】:

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

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

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

我的 Pin 图控制器

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'

这是否与 Amazon Web Services 相关联?

【问题讨论】:

  • pins 表中有user_id 吗?如果你这样做了,你是否运行了迁移?
  • heroku run rake db:migrate+1
  • 原来我还没有运行 heroku run rake db:migrate。现在已经做到了。但是遇到了另一个问题(在上面添加)
  • 好的,我设法解决了最后一个错误。 :)。我进入并在 config/environments/production.rb 中将 :bucket => ENV['S3_BUCKET_NAME'], 更改为 :bucket => ENV['AWS_BUCKET']。谢谢大家!

标签: ruby-on-rails activerecord heroku


【解决方案1】:

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

heroku run rake db:migrate

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

heroku config:set S3_BUCKET_NAME=nameOfYourBucket

【讨论】:

    【解决方案2】:

    我会参加新的行动

    @pin = Pin.new
    

    您正在使用 Create 操作从关系中添加值,所以它应该可以正常工作

    除此之外,在 New 中使用 .new,在 Create 中使用 .build

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 2021-11-13
      • 1970-01-01
      相关资源
      最近更新 更多