2011-07-14 71 views
2

我想從色器件添加current_user.id到另一種模式......色器件軌添加USER_ID到另一種模式

問題型號:

class Question < ActiveRecord::Base 
    belongs_to :user 
    attr_accessible :content 

    validates :content, :presence => true 
end 

用戶模型:

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

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 
end 

我的問題遷移:

class CreateQuestions < ActiveRecord::Migration 
    def change 
    create_table :questions do |t| 
     t.string :content 
     t.date :deadline 
     t.integer :user_id 

     t.timestamps 
    end 
    end 
end 

在我的問題控制器:

def create 

    params[:question][:user_id]=current_user.id 

    @question = Question.new(params[:question]) 
    if @question.save 
     redirect_to questions_path, :notice => "Successfully created question." 
    else 
     setup_questions 
     render :index 
    end 
    end 

當我保存current_user.id不會被記錄在我的數據庫的問題?

回答

7

試試這個:

def create 

    @question = Question.new(params[:question]) 
    @question.user_id = current_user.id 
    if @question.save 
    redirect_to questions_path, :notice => "Successfully created question." 
    else 
    setup_questions 
    render :index 
    end 

end 
+0

謝謝!如果我只知道... – Thijs

相關問題