我是初学者,现在已经学习Ruby on Rails大约4个月了。
我正在构建一个基本的wiki应用程序,该应用程序应该允许私有wiki。现在,当用户创建wiki时,rails不会为wiki分配用户ID。当我在rails控制台中查看wiki时,一切都在那里 - created_at,title,body,wiki id - 但user_id总是为零。我怎样才能解决这个问题?
这是我的Wiki控制器:
class WikisController < ApplicationController
def index
@wikis = Wiki.all
end
def new
@wiki =Wiki.new
authorize @wiki
end
def show
@wiki = Wiki.find(params[:id])
end
def edit
@wiki = Wiki.find(params[:id])
authorize @wiki
end
def update
@wiki = Wiki.find(params[:id])
authorize @wiki
if @wiki.update_attributes(wiki_params)
flash[:notice] = "Wiki was updated."
redirect_to @wiki
else
flash[:error] = "There was an error updating the wiki."
render :edit
end
end
def create
@wiki = Wiki.new(wiki_params)
authorize @wiki
if @wiki.save
flash[:notice] = "Wiki was saved."
redirect_to @wiki
else
flash[:error] = "There was an error saving the wiki. Please try again."
render :new
end
end
def destroy
@wiki = Wiki.find(params[:id])
name = @wiki.title
if @wiki.destroy
flash[:notice] = "\"#{name}\" was deleted."
redirect_to wikis_path
else
flash[:error] = "There was an error deleting the wiki."
redirect_to @wiki
end
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :private, :user_id)
end
......这是我的模特:
class User < ActiveRecord::Base
def admin?
role == 'admin'
end
def member?
role == 'member'
end
def premium_member?
role == 'premium_member'
end
has_many :collaborations
has_many :wikis, through: :collaborations
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
end
加入表...
class Collaboration < ActiveRecord::Base
belongs_to :user
belongs_to :wiki
end
维基模式......
class Wiki < ActiveRecord::Base
has_many :collaborations
has_many :users, through: :collaborations
end
最后,架构:
create_table "collaborations", force: true do |t|
t.integer "user_id"
t.integer "wiki_id"
end
create_table "users", force: true do |t|
t.string "name"
t.string "role", default: "member", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
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"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "wikis", force: true do |t|
t.string "title"
t.string "body"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "private", default: false
end
答案 0 :(得分:0)
如果您说维基has_many :users, through: :collaborations
,那么维基上的user_id
列将不会被自动填充。相反,它应该只将wiki_id
与user_id
一起保存在collaborations
表中。否则,wiki只能有一个用户,对吗?
如果您真的想在wiki表列中保存user_id
,可以在wiki模型的callback中进行保存。