Devise - current_user是作者,Rails 3.0

fig*_*lla 1 devise ruby-on-rails-3

我使用Rails 3.0和Devise.

我试图找出最佳做法,使用户中的current_logged自动成为帖子的"作者".

我是否使用隐藏的表单字段?或者我可以以某种方式在控制器中应用此逻辑?

-

所以我的例子:

Bob已登录,他在网站上创建了一个帖子.

当他创建帖子时,他不必填写作者字段,它只是使用设计提供应用程序布局模板的"current_user".


我找了一个直接的答案,但我找不到.

Sni*_*per 8

假设你有

class User < ActiveRecord::Base
  has_many :posts
  ...
end

class Post < ActiveRecord::Base
  belongs_to :user
  ...
end
Run Code Online (Sandbox Code Playgroud)

您可以在控制器中执行"创建"操作

@post = current_user.posts.new(params[:post])
@post.save
Run Code Online (Sandbox Code Playgroud)