1

I upgraded my app from 6.1 to rails 7. I removed webpack from the project.

actiontext/trix isn't working locally or in production. all I get is a white box that i can't even insert text into. pretty sure i followed all the instructions correctly but i'm not sure where i went wrong. Any guidance on this would be highly appreciated! this app is running on rate.photos

rails server errors:

ActionController::RoutingError (No route matches [GET] "/assets/@rails/activestorage"):
  
  
ActionController::RoutingError (No route matches [GET] "/assets/@rails/actiontext"):
  
  
ActionController::RoutingError (No route matches [GET] "/assets/@rails/ujs"):

similarly in Heroku logs, a whole bunch of these:

Started GET "/blog/wp-includes/wlwmanifest.xml" 
FATAL -- ActionController::RoutingError (No route matches [GET] "/blog/wp-includes/wlwmanifest.xml"):

same thing both locally and in production it shows trix-editor

support.rb

class Support < ApplicationRecord

    has_rich_text :content
    belongs_to :user
    validates :name, presence: true
    validates :content, presence: true
end

importmap.rb

pin "application", preload: true
pin "@rails/activestorage", to: "activestorage.esm.js"
pin "local-time" # @2.1.0
pin "trix"
pin "@rails/actiontext", to: "actiontext.js"
pin "sortablejs", to: "https://cdn.jsdelivr.net/npm/sortablejs@1.14.0/modular/sortable.esm.js"
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading-js", preload: true
# pin "@rails/actioncable", to: "actioncable.esm.js"
pin_all_from "app/javascript/channels", under: "channels"
pin_all_from "app/javascript/controllers", under: "controllers"

application.scss

@import 'trix/dist/trix.css';
@import 'actiontext';

 .img {
    width: 300px; /* You can set the dimensions to whatever you want */
    height: 300px;
    object-fit: cover;
}

application.html.erb

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_importmap_tags %>

    <title>Rate Photos</title>
    <%= favicon_link_tag asset_path('favicon.ico') %>
  </head>

  <body>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    <%= render 'home/header' %>
    <div class="container">
    <%= render 'layouts/messages' %>
      <%= yield %>
    </div> 
  </body>

</html>

added the following in my config/environment/poduction.rb

 config.serve_static_files = false

app/javascript/application.js

// import "./@rails/actionable"
import "@rails/activestorage"

import "./trix"
import "@rails/actiontext"

import Rails from "./@rails/ujs"
import Turbolinks from "./turbolinks"
import * as ActiveStorage from "./@rails/activestorage"
import "./channels"
import LocalTime from "./local-time"

LocalTime.start()
Rails.start()
Turbolinks.start()
ActiveStorage.start()
import "trix"
import "@rails/actiontext"

app/views/supports/_form.html.erb

<%= form_with(model: support) do |form| %>
  <% if support.errors.any? %>
    <div style="color: red">
      <h2><%= pluralize(support.errors.count, "error") %> prohibited this support from being saved:</h2>

      <ul>
        <% support.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="mb-3">
    <%= form.label "Support Type: ", class:"form-label" %>
    <%= form.select :name, options_for_select([["Bug", ""], "Question", "Feedback", "Review", "Other"]), class: "form-select", id:"exampleFormControlInput1" %>
  </div>

  <div class="mb-3">
    <%= form.rich_text_area :content, class:"form-control", id:"exampleFormControlTextarea1", rows:"3", placeholder:"Please provide as much details as possible"  %>
  </div>

  <div class="field form-group mb-3">
    <%= form.number_field :user_id, id: :support_user_id, class:"form-control", value:current_user.id, type: :hidden%>
  </div>

  <div class="actions d-grid gap-2 mb-3"">
    <%= form.submit class:"btn btn-primary" %>
  </div>
<% end %>
  • Did you `pin` `trix` and `@rails/actiontext` inside `config/importmap.rb`? – radiantshaw Jan 09 '22 at 14:16
  • Just updated the post to show importmap.rb. i did pin them both yes and a whole bunch of other things. – Mohamed A. Negm Jan 09 '22 at 17:03
  • Based on the error, it looks like Rails is trying to initialise `ActionController`, which it shouldn't when trying to serve static assets from the `public/assets` directory. Can you check if `config.public_file_server.enabled` returns `true` for the environment of your choice? – radiantshaw Jan 09 '22 at 17:45
  • @radiantshaw i don't have `config.public_file_server.enabled = true` i have this instead `config.public_file_server.headers = {"Cache-Control" => "public, max-age=#{2.days.to_i}"}` in development. and i have ` config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?` in production. Also i have ran `rake asset:clobber` and `rake asset:precompile`. This error is no longer in the server however the problem still persists. – Mohamed A. Negm Jan 09 '22 at 18:03
  • Is the `RAILS_SERVE_STATIC_FILES` environment variable set to any value? – radiantshaw Jan 09 '22 at 18:58
  • I don't know to be honest, i searched for `RAILS_SERVE_STATIC_FILES` in VScode using both command P & command shift f and nothing came back except for the one time it showed up in config/environments/development.rb – Mohamed A. Negm Jan 09 '22 at 19:09
  • The environment variables you set are not part of the codebase. Your codebase only consumes them. They are set for the server process. What you need to do is open the production console, and check the value of `ENV['RAILS_SERVE_STATIC_FILES']`. Alternatively, you start your development Rails server as `env RAILS_SERVE_STATIC_FILES=1 rails s` and check if the assets are now being served properly. – radiantshaw Jan 10 '22 at 05:30

0 Answers0