-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
還沒在LinksController#新 – carversmind
工作同樣的錯誤的ActiveRecord :: UnknownAttributeError加入這一行'捆綁高管耙分貝後:遷移:redo'或'束EXEC耙db:drop:all db:create:all db:migrate'。您應該能夠驗證鏈接表上是否有'user_id'列。 –
我看到我們放下數據庫,並從地面重新創建它,因爲我添加了用戶。但是,如果你有時間或知道我在哪裏可以找到答案爲什麼我不能只添加:用戶到當前的遷移? – carversmind