← Back to Upcase

How do I add the user_id to a Posts table when I'm using rails_admin to create the posts?


(Jameson Trinker) #1

I’m building a blog in rails with the back-end editor done in rails_admin and ckeditor. A User has many Posts, and Posts belong to Users.

The current_user must be logged in to the back-end in order to create a new post. However, since there is no form or view to create new post, my usual way of doing this doesn’t work:

class PostsController < ApplicationController
def create
  @post = current_user.posts.new(project_parameters)
end

def show
  @post = Post.find(params[:id])
end

private

def project_parameters
  params.require(:post).permit(:id)
end

end

So how do do I add the current_user’s id when a new post is created from the rails_admin backend?

Thanks!