I have this as my create method inside my PostsController
class PostsController < ApplicationController
def create
@user = current_user
@post = current_user.posts.build(params[:post])
if @post.save
flash[:success] = "Post created!"
redirect_to root_path
else
@feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
render 'pages/home'
end
end
I’d like to change it slightly to also set :user_id => params[:post][:user_id]. I’m trying this but it doesn’t include the current_user so I am wondering how are these create methods different and how would I keep the same functionality in current_user.posts.build(params[:post]) with Post.new ?
def create
@post = Post.new :user_id => params[:post][:user_id]
@post.update_attributes params[:post]
if @post.save
flash[:success] = "Post created!"
redirect_to root_path
else
@feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
render 'pages/home'
end
end
end
in your post model belongs_to :user