Using Ruby on Rails 6, after the user saves their story(using Action Text to type their story), they will be redirected to the show
view where the story can be read. However, nothing is showing up and when checking the database, nothing seems to be saved.
After going through the documentation and watching some videos, I removed the body
column from my Story
table to be stored by ActionText.
The model:
class Story < ApplicationRecord
# declare that body has a rich text field
has_rich_text :body
end
The controller:
def new
@story = Story.new
end
def create
@story = Story.new(params[:story_params])
if @story.save
redirect_to @story
else
render 'new'
end
end
def show
@story = Story.find(params[:id])
end
private
def story_params
params.require(:story).permit(:heading, :summary, :body)
end
The form from new.html.erb
<%= form_with(model: @story) do |form| %>
<%= form.label :heading %> <br>
<%= form.text_area :heading %>
<br>
<%= form.label :summary %> <br>
<%= form.text_area :summary %>
<br>
<%= form.label :body %>
<%= form.rich_text_area :body %>
<br>
<%= form.submit %>
<% end %>
I expected the heading
and summary
to be stored in the Story table while ActionText handles the body
.
Here's what happens when creating and saving a story.
As seen here, nothing is saved in the Story table.