Search code examples
storagerails-activestorageruby-on-rails-7actiontext

ActionText not being stored on create


I'm building Rails app where I want administrators to be able to use ActionText to create rich text contents. I've set up the app according to manual, Image attachments work, but rich texts dont, I can't see them in my local DB. What is wrong, what am I missing? P.s.: I want multiple actiontexts in one form, but for now I'm trying to get just one working...

Model:

class Course < ApplicationRecord
    has_and_belongs_to_many :admins, join_table: 'courses_admins', class_name: 'Admin'
    has_many :lessons
    has_many :course_runs
    has_many :course_interests

    has_rich_text :what_you_learn
    # has_rich_text :skillset
    # has_rich_text :process
    # has_rich_text :harmonogram
    # has_rich_text :student_assignment

    has_one_attached :image
end

Controller POST method:

# POST /courses or /courses.json
def create
    @course = Course.new(course_params)
    @course.image.attach(course_params[:image])


    respond_to do |format|
      if @course.save
        format.html { redirect_to course_url(@course), notice: "Course was successfully created." }
        format.json { render :show, status: :created, location: @course }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @course.errors, status: :unprocessable_entity }
      end
    end
  end

And typed params:

def course_params
    params.require(:course).permit(
      :name,
      :annotation,
      :price,
      :capacity,
      :image,
      :what_you_learn
      # :skillset,
      # :process,
      # :harmonogram,
      # :student_assignment
    )
end

Solution

  • For anyone interested, the problem was in .html.erb markup file, as I copied the rich_text_area tag without proper walkthrough of its attributes.

    Changed

    <%= form.rich_text_area :what_you_learn, class: 'form-control', name: "article-text", input_html: { rows: 10 } %>

    to

    <%= form.rich_text_area :what_you_learn, class: 'form-control', input_html: { rows: 10 } %>

    The content of the rich_text was then posted not as 'article-text', but properly inside params[:what_you_learn].