Reputation: 2849
I am trying to get the user_id of the user that is logged in. So a user will create a blog post and it will capture the id of the user when creating the blog post.
I know how to do this using nested resources but how can I get the user_id when creating a new blog post without nested resources?
the routes would look like this
resources :blogs
I also tried adding hidden field to blog form _blog.html.erb
<%= f.hidden_field :site_id, :value => @blog.site_id %>
The association is set up correctly I am just trying to see how I can get the user_id without nested resources. Any suggestions?
Upvotes: 0
Views: 97
Reputation: 31786
Pull the user id off the currently logged in user in the BlogsController#create method.
Example (Note that how you access the user_id will depend on how you're doing authentication)
class BlogsController < ApplicationController
def create
@blog = Blog.new params[:blog]
@blog.user_id = current_user.id
if @blog.save
...
end
end
end
Upvotes: 3
Reputation: 7733
if you are using devise/authlogic for authentication then you can easily get user_id using current_user
method provided
Upvotes: 1