0

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.

Sam Lee
  • 65
  • 8
  • "After going through the documentation and watching some videos, I removed the body column from my Story table to be stored by ActionText." I don't understand why did you do this? Where do you want this `body` content to be stored? – Marek Lipka Sep 09 '19 at 06:37
  • The `body` is the content of the story. Initially it was in the Story table as `body:text`. After learning that ActionText has its own table when watching this [video](https://www.youtube.com/watch?v=HJZ9TnKrt7Q&t=662s), I decided to let ActionText store the body by declaring `has_rich_text :body` in the Story model as it seemed to be the best practice. – Sam Lee Sep 09 '19 at 06:47
  • Actually, you only show GET for show and no log for create. That might be more useful. – katafrakt Sep 09 '19 at 07:04

1 Answers1

0

You should actually use your story_params method:

@story = Story.new(story_params)
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91