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
精彩评论