设计rails将user_id添加到另一个模型

发布于 2024-11-23 15:35:44 字数 1356 浏览 0 评论 0原文

我想将 devise 中的 current_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 不会记录在我的数据库中吗?

I want to add the current_user.id from devise to another model...

question model:

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

  validates :content, :presence => true
end

user model:

    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

my question migration:

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

In my questions controller:

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

When i save the question the current_user.id won't be recorded in my database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-11-30 15:35:44

试试这个:

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

Try this instead:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文