2016-08-18 26 views
-1

嗨,大家好,我是Ruby的新手,试图构建一个网站,发生这种情况。LinksController中的ActiveRecord :: UnknownAttributeError#new

我真的很满意为什么这不起作用。我一直在这里阅读其他人的线索没有找到一个好的解决方案。

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

LinksController

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 

型号用户

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 Link < ActiveRecord::Base 
    belongs_to :user 
end 

我真的没有线索,如果我需要包括更多。

编辑

DB /迁移文件...

我可能会丢失一些,但是当我 “耙分贝:迁移” 什么也没发生..

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 

AND

_devise_create_users

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 

回答

0

CreateLinks需要行:

t.references :user 
+0

还没在LinksController#新 – carversmind

+0

工作同样的错误的ActiveRecord :: UnknownAttributeError加入这一行'捆绑高管耙分贝后:迁移:redo'或'束EXEC耙db:drop:all db:create:all db:migrate'。您应该能够验证链接表上是否有'user_id'列。 –

+0

我看到我们放下数据库,并从地面重新创建它,因为我添加了用户。但是,如果你有时间或知道我在哪里可以找到答案为什么我不能只添加:用户到当前的迁移? – carversmind

相关问题