Rails 6 ActionText rich_text_area正常工作,在布局中呈现时不显示 [英] Rails 6 ActionText rich_text_area works normally, does not appear when rendered in a layout
问题描述
我正在渲染一个利用ActionText输入rich_text_area的表单,它是Trix WYSIWYG富文本编辑器的Rails实现.在开始重构视图以使用布局之前,它一直运行良好,在该布局中,表单内容可以生成不同的表单.表单为rich_text_areas呈现了很好的 except ,它们都消失了.删除使该布局所需的代码可使它恢复正常.我的代码如下.
我使用以下代码将表单呈现为部分表单:
<%= stylesheet_link_tag "form" %>
<%= form_with model: @family, class: "form-group" do |f| %>
<%= label_tag(:banner_image, 'Select a Banner Image')%>
<%= f.file_field :banner_image%>
<br>
<br>
<%= label_tag(:zh_family_name, 'Chinese Family Name') %>
<%= f.text_field :zh_family_name %>
<br>
<br>
<%= label_tag(:en_family_name, 'English Family Name') %>
<%= f.text_field :en_family_name %>
<br>
<br>
<%= label_tag(:en_summary, 'Summary') %>
<%= f.text_field :en_summary %>
<br>
<br>
<%= label_tag(:zh_summary, 'Summary Chinese') %>
<%= f.text_field :zh_summary %>
<br>
<br>
<%= label_tag(:en_content, 'Content') %>
<%= f.rich_text_area :en_content_rt %>
<br>
<br>
<%= label_tag(:zh_content, '内容') %>
<%= f.rich_text_area :zh_content_rt %>
<br>
<br>
<%= label_tag(:service_id, 'Select a related service') %>
<%= f.collection_select(:service_id, Service.all, :id, :en_title) %>
<br>
<br>
<%= f.submit %>
<% end %>
它工作正常,并且富文本区域可见,并且可以将表单作为部分内容加载到edit.html.erb文件中时使用,如下所示:
<%= render "partials/family_form" %>
但是我想将其渲染为名为"editor"的布局的一部分,因此我将edit.html.erb更改为:
<% content_for :form do %>
<h1>Edit the <em><%= @family.en_family_name %></em> Testimonial </h1>
<%= render "partials/family_form" %>
<% end %>
将render layout: 'editor'
添加到控制器中的编辑功能,并添加如下所示的布局:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<%= stylesheet_link_tag "editor" %>
<%= yield :head %>
</head>
<body>
<div class="main-container">
<div class="sidenav">
<%= render "partials/sidenav" %>
</div>
<div class="content">
<%= yield :form %>
</div>
</div>
</body>
</html>
我认为我已将其范围缩小为问题的根源,因为直接将表单代码粘贴到editor.html.erb布局文件中会产生相同的错误:除了富文本区域外,其他所有内容都可以正常显示.
我尚未在Trix存储库上看到此问题,并尝试将require 'action_text'
和ActionText帮助程序手动添加到相关控制器,但无济于事.任何帮助将不胜感激,谢谢.
您是否通过Webpacker或资产管道设置了action_text
?
如果使用Webpacker进行设置,则可能必须添加<%= javascript_pack_tag 'application' %>
如果您使用资产管道进行设置,然后以 @MurifoX 进行设置,则可能需要<%= javascript_include_tag 'application' %>
.>
I'm rendering a form that utilizes the ActionText input rich_text_area, which is the Rails implementation of the Trix WYSIWYG rich text editor. It's been working fine until I started refactoring my views to use a layout, where the form content can yield different forms. The form renders fine except for the rich_text_areas, which both disappear. Removing the code required to make this a layout returns it back to normal. My code is below.
I'm rendering a form as a partial with the following code:
<%= stylesheet_link_tag "form" %>
<%= form_with model: @family, class: "form-group" do |f| %>
<%= label_tag(:banner_image, 'Select a Banner Image')%>
<%= f.file_field :banner_image%>
<br>
<br>
<%= label_tag(:zh_family_name, 'Chinese Family Name') %>
<%= f.text_field :zh_family_name %>
<br>
<br>
<%= label_tag(:en_family_name, 'English Family Name') %>
<%= f.text_field :en_family_name %>
<br>
<br>
<%= label_tag(:en_summary, 'Summary') %>
<%= f.text_field :en_summary %>
<br>
<br>
<%= label_tag(:zh_summary, 'Summary Chinese') %>
<%= f.text_field :zh_summary %>
<br>
<br>
<%= label_tag(:en_content, 'Content') %>
<%= f.rich_text_area :en_content_rt %>
<br>
<br>
<%= label_tag(:zh_content, '内容') %>
<%= f.rich_text_area :zh_content_rt %>
<br>
<br>
<%= label_tag(:service_id, 'Select a related service') %>
<%= f.collection_select(:service_id, Service.all, :id, :en_title) %>
<br>
<br>
<%= f.submit %>
<% end %>
It works fine and the rich text areas are visible and can be used when this form is loaded in to the edit.html.erb file as a partial like so:
<%= render "partials/family_form" %>
But I'd like to render it as part of a layout called 'editor', thus I change the edit.html.erb to this:
<% content_for :form do %>
<h1>Edit the <em><%= @family.en_family_name %></em> Testimonial </h1>
<%= render "partials/family_form" %>
<% end %>
add render layout: 'editor'
to the edit function in the controller, and add a layout that looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<%= stylesheet_link_tag "editor" %>
<%= yield :head %>
</head>
<body>
<div class="main-container">
<div class="sidenav">
<%= render "partials/sidenav" %>
</div>
<div class="content">
<%= yield :form %>
</div>
</div>
</body>
</html>
I think I've narrowed it down to the render layout: 'editor'
code being the source of the problem, since directly pasting the form code into the editor.html.erb layout file produces the same error: everything renders fine except for the rich text areas.
I haven't seen this issue reported on the Trix repo and have tried manually adding require 'action_text'
and the ActionText helper to the relevant controller, to no avail. Any help would be greatly appreciated, thanks.
Did you setup your action_text
with with Webpacker or with assets pipeline?
If you set it up with Webpacker you may have to add <%= javascript_pack_tag 'application' %>
If you set it up with assets pipeline then as @MurifoX mentioned, you may need the <%= javascript_include_tag 'application' %>
.
这篇关于Rails 6 ActionText rich_text_area正常工作,在布局中呈现时不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!