Skip to content

Instantly share code, notes, and snippets.

@adrienpoly
Created October 4, 2018 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adrienpoly/e318bd9f559d487cffb11eb84ead2655 to your computer and use it in GitHub Desktop.
Save adrienpoly/e318bd9f559d487cffb11eb84ead2655 to your computer and use it in GitHub Desktop.
Step by Step guide to test Rails ActiveText

Step by Step guide to test Rails ActiveText

You might have seen the video of DHH about ActionText upcoming feature in Rails 6. This is a step by step guide for creating the exact same example as in the video.

⚠️ If like me you never ran an edge version of Rails before and wonder how to do it then this is for you

Create a new app from the Edge version of Rails

  1. Clone the Rails repo

    git clone https://github.com/rails/rails.git
    cd rails
    bundle install
    
  2. Create a new app with Rails Edge, YABE (YABE: Yet Another Blog Example) we will want to create a new app within the same directory and using the locally installed Rails generator

    #move out of rails folder first
    cd ..
    
    #create the app with the local rails generator and the edge flag to use GH master branch
    ./rails/railties/exe/rails new yabe --edge
    
    cd yabe
  3. Start the app

    ⚠️ remember to always use the local rails command: ./bin/rails

    ./bin/rails s

    visit http://localhost:3000

    you should now have the rails startup screen showing

    Rails version: 6.0.0.alpha

Installing ActionText

  1. Add the ActionText gem (and image variants for active Storage):

    # Gemfile
    gem "actiontext", github: "rails/actiontext", require: "action_text"
    gem "image_processing", "~> 1.2" # for Active Storage variants
  2. Install gem, assets, npm dependency, and migrations

    bundle install
    ./bin/rails action_text:install
    ./bin/rails db:migrate
    
  3. Scaffold Post

    ./bin/rails g scaffold post title:string
    ./bin/rails db:migrate
    
  4. Adding a rich text field to Post

    # app/models/post.rb
    class Post < ApplicationRecord
      has_rich_text :content
    end

    Active Text brings a polymorphic model under the hood for managing RichText

  5. Add a new content

    <%= form.rich_text_area :content %> rich_text_area is the new content type to display Trix

    <%# app/views/posts/_form.html.erb %>
    <%= form_with(model: post) do |form| %><div class="field">
        <%= form.label :content %>
        <%= form.rich_text_area :content %>
      </div><% end %>
  6. finally display the rich text on a page:

    <%= @post.content %>
  7. To accept the rich text content, all you have to do is permit the referenced attribute: update the

    class MessagesController < ApplicationController
      def create
        message = Message.create! params.require(:message).permit(:title, :content)
        redirect_to message
      end
    end
  8. Start bloging

    `http://localhost:3000/posts`
    
@jakehockey10
Copy link

Where are the tests?

@tux255
Copy link

tux255 commented Jun 23, 2021

Where are the tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment