开发者

Devise - current_user is author, Rails 3.0

开发者 https://www.devze.com 2023-02-11 00:40 出处:网络
Iam using Rails 3.0 and Devise. I am trying to figure out the best practice to make the current_logged in user automatically the \"author\" o开发者_高级运维f a Post.

Iam using Rails 3.0 and Devise.

I am trying to figure out the best practice to make the current_logged in user automatically the "author" o开发者_高级运维f a Post.

Do I use a hidden form field? Or can I somehow apply this logic in the controller?

--

So my example:

Bob is logged in and he creates a Post on the site.

When he creates the post he doesn't have to fill in the Author field, it simply uses the "current_user" that devise provides the application layout template.


I have looked for a straight answer, but I cannot find.


Assuming you have

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

class Post < ActiveRecord::Base
  belongs_to :user
  ...
end

You can do this in your controller, on the "create" action

@post = current_user.posts.new(params[:post])
@post.save
0

精彩评论

暂无评论...
验证码 换一张
取 消