【问题标题】:Rails 6 ActionText isn't working on Heroku (undefined method 'rich_text_area_tag')Rails 6 ActionText 不适用于 Heroku(未定义的方法 'rich_text_area_tag')
【发布时间】:2019-12-29 00:11:56
【问题描述】:

我正在使用 ActionText 编辑一个段落,它在本地完美运行,但是当我将它部署到 Heroku 具有rich_text_area 表单的页面时,即使我遵循了 rails 指南,它也会抛出一个错误,指出未定义的方法 rich_text_area_tag。我以为我需要在生产环境中配置 Active Storage,但事实并非如此。

这是我在 Heroku 的日志中得到的信息: ActionView::Template::Error (undefined method 'rich_text_area_tag' for #<#<Class> Did you mean? rich_text_area)

<%= f.label :something, class:'label' %>
<%= f.rich_text_area :something %>

【问题讨论】:

  • 你的错误是undefined method 'rich_text_area_tag',但你的代码只有rich_text_area。您是否仅使用 f.rich_text_area 对其进行了更新?
  • @allenbrkn 感谢您的评论,我在代码中将其命名为f.rich_text_area,但我不知道为什么要添加 tag,如果您的意思是我应该删除:something 会导致错误。
  • 部署后是否尝试运行heroku run bundle exec rake db:migrateheroku run bundle exec rake assets:precompile等命令?
  • @allenbrkn 我尝试了这两种方法,但没有任何改变,仍然出现相同的错误。
  • 你能运行heroku run rails -vheroku run ruby -v 看看Heroku 是否在使用你喜欢的Rails/Ruby 版本。大多数时候它会正确使用,但以防万一。另外,你能更新你完整的form 代码吗?

标签: ruby-on-rails heroku ruby-on-rails-6 actiontext


【解决方案1】:

我在网上找到了这个,对我很有帮助:

https://github.com/JoeWoodward/spree_helper_issue

我不确定这是否是正确的方法,但这是一种临时解决方法。

解决方案:

application_controller.rb 中输入以下内容:

require 'action_text'

class ApplicationController < ActionController::Base
  helper ActionText::Engine.helpers

  ...
end

helper 的文档:https://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper

【讨论】:

  • 感谢@Violeta ?我在app/controllers/application_controller.rb 中添加了require 'action_text'helpers ActionText::Engine.helpers,因为它是in 链接
  • 很高兴能够提供帮助!
  • 工作就像一个魅力,小变化:helper ActionText::Engine.helpers(没有“s”)我相信这是一个错字:)
【解决方案2】:

首先确保您在 config/application.rb 中有这一行:

require 'rails/all'

如果是 rails 6 应用程序 action_text/engine 应该被加载。

如果不是,则可能是 zeitwerk 未加载。当您有一个更新到 rails 6 的 rails 5.2 应用程序时,就会发生这种情况。

在同一个文件 (config/application.rb) 中,您必须将 config.load_defaults 更改为 6.0:

config.load_defaults 6.0

如果您想知道后台发生了什么,请查看第 125 行的 this link

【讨论】:

  • 感谢您花时间查看它,所以我检查了config/application.rb 我有这两行require 'rails/all'config.load_defaults 6.0 唯一对我有用的解决方案是我必须添加require 'action_text' 但我最终手动添加了 trix-editor 标签。 @TopperH
  • 您是否因为某些原因不使用 zeitwerk?
  • 我在config/application.rb @TopperH 中有config.autoloader = :zeitwerk
【解决方案3】:

为避免混淆ApplicationController 代码,您可以使用初始化程序:

即添加新文件config/initializers/action_text.rb:

ActiveSupport.on_load(:action_view) do
  include ActionText::ContentHelper
  include ActionText::TagHelper
end

灵感来自 p8 在封闭的 Rails issue 中的评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2020-10-12
    • 2012-08-11
    • 2013-01-04
    相关资源
    最近更新 更多