Reputation:
I'm trying to build a really simple backend only rails app with a post class. I can't seem to get my 'Posts' to persist because the rich_text field doesn't seem to be working. My actually class has more than this, the only error I'm encountering is around the rich_text. If I don't require this field I can save a post, but the rich_text is always null.
Post Class
class Post < ApplicationRecord
has_rich_text :content
validates :content, presence: true
end
Controller
class PostsController < ApplicationController
before_action :set_post, only: [:create, :show, :update, :destroy]
def create
@post = Post.new params.require(:post).permit(:content)
if @post.save
render json: @post, status: :created, location: @post
else
render json: @post.errors, status: :unprocessable_entity
end
end
end
test:
test "should save post with valid attributes" do
post = Post.new(
content: "<div>Sample Content</div>"
)
puts "DEBUG: content attribute before save: #{post.content.body.to_s.inspect}"
assert post.save, "Could not save post with valid attributes. Errors: #{post.errors.full_messages.join(', ')}"
end
Error
Error:
PostTest#test_should_save_post_with_valid_attributes:
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR: null value in column "content" of relation "posts" violates not-null constraint
I've tried looking at the docs, reading some posts about this issue, and asking ChatGPT for help, but I'm not able to solve it.
Any tips for how to fix this?
I'm trying to save a post with rich_text. I've read a few other issues around rails 7 with rich_text but they made a lot of suggestions about fixing the front end and this is backend only. I've tried deleting everything and starting over. I've tried asking ChatGPT. No solution yet.
Upvotes: 0
Views: 105