Ruby on rails ActiveRecord::LinksController中的UnknownAttributeError#新建

Ruby on rails ActiveRecord::LinksController中的UnknownAttributeError#新建,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,嗨,伙计们,我是Ruby的新手,尝试建立一个网站,这种情况发生了 我真的不明白为什么这不起作用。我一直在这里阅读其他人的帖子,但并没有找到一个好的解决方案 unknown attribute 'user_id' for Link. Rails.root: c:/sites/rubbit Application Trace | Framework Trace | Full Trace app/controllers/links_controller.rb:18:in `new' Request

嗨,伙计们,我是Ruby的新手,尝试建立一个网站,这种情况发生了

我真的不明白为什么这不起作用。我一直在这里阅读其他人的帖子,但并没有找到一个好的解决方案

unknown attribute 'user_id' for Link.

Rails.root: c:/sites/rubbit

Application Trace | Framework Trace | Full Trace
app/controllers/links_controller.rb:18:in `new'
Request
我想问题是从Links.new更改为current_user.Links.build后开始的

链接控制器

class LinksController < ApplicationController
  before_action :set_link, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_user!, :except => [:index, :show]

  # GET /links
  # GET /links.json
  def index
    @links = Link.all
  end

  # GET /links/1
  # GET /links/1.json
  def show
  end

  # GET /links/new
  def new
    @link = current_user.links.build
  end

  # GET /links/1/edit
  def edit
  end

  # POST /links
  # POST /links.json
  def create
    @link = current_user.links.build(link_params)

    respond_to do |format|
      if @link.save
        format.html { redirect_to @link, notice: 'Link was successfully created.' }
        format.json { render :show, status: :created, location: @link }
      else
        format.html { render :new }
        format.json { render json: @link.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /links/1
  # PATCH/PUT /links/1.json
  def update
    respond_to do |format|
      if @link.update(link_params)
        format.html { redirect_to @link, notice: 'Link was successfully updated.' }
        format.json { render :show, status: :ok, location: @link }
      else
        format.html { render :edit }
        format.json { render json: @link.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /links/1
  # DELETE /links/1.json
  def destroy
    @link.destroy
    respond_to do |format|
      format.html { redirect_to links_url, notice: 'Link was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def link_params
      params.require(:link).permit(:title, :url)
    end
end
类链接控制器[:索引,:显示]
#获取/链接
#GET/links.json
def索引
@links=Link.all
结束
#获取/links/1
#GET/links/1.json
def秀
结束
#获取/链接/新建
def新
@link=当前_user.links.build
结束
#获取/链接/1/编辑
定义编辑
结束
#帖子/链接
#POST/links.json
def创建
@link=当前用户.links.build(链接参数)
回应待办事项|格式|
如果@link.save
format.html{将_重定向到@link,注意:'链接已成功创建。}
format.json{render:show,status::created,location:@link}
其他的
format.html{render:new}
format.json{render json:@link.errors,status::unprocessable_entity}
结束
结束
结束
#补丁/放置/链接/1
#PATCH/PUT/links/1.json
def更新
回应待办事项|格式|
如果@link.update(link_参数)
format.html{将_重定向到@link,注意:'链接已成功更新。}
format.json{render:show,status::ok,location:@link}
其他的
format.html{render:edit}
format.json{render json:@link.errors,status::unprocessable_entity}
结束
结束
结束
#删除/links/1
#删除/links/1.json
def销毁
@毁灭
回应待办事项|格式|
format.html{重定向到链接,注意:'链接已成功销毁。}
format.json{head:no_content}
结束
结束
私有的
#使用回调在操作之间共享公共设置或约束。
def set_链接
@link=link.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def link_参数
参数require(:link).permit(:title,:url)
结束
结束
模型用户

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :links
end
class用户
模型链接

class Link < ActiveRecord::Base
    belongs_to :user
end
class链接
我真的不知道我是否需要包括更多

编辑

数据库/迁移文件

我可能遗漏了一些,但当我“rake db:migrate”时,什么也没有发生

2个文件

_create_links
    class CreateLinks < ActiveRecord::Migration
      def change
        create_table :links do |t|
          t.string :title
          t.string :url

          t.timestamps null: false
        end
      end
    end
创建链接
类CreateLinks

_设计并创建用户

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end
classdeveliecCreateUsers
CreateLinks
需要一行:

t.references :user

仍然无法工作相同的错误ActiveRecord::UnknownAttributeError in LinksController#new添加此行后
bundle exec rake db:migrate:redo
bundle exec rake db:drop:all db:create:all db:migrate
。您应该能够验证links表上是否有
user\u id
列。我看到我们删除了数据库并从地面重新创建它,因为我添加了user。但是如果你有时间或者知道我在哪里可以找到答案,为什么我不能将:user添加到当前迁移中?@carversmind迁移系统已经就位,允许你随着时间的推移更改生产和暂存数据库。运行迁移时,会在
schema\u versions
表中插入一行,其时间戳与迁移匹配。一旦有了这一行,Rails就不会重新运行该数据库——它假定没有进行任何更改。在开发中,您可以使用我上面提到的命令。