我有一個奇怪的問題。
我最近在我的Rails應用程式中安裝了Trix。一切都很順利,直到我試圖發布一篇博文,發現像strong>和div>這樣的html元素被當作實際的文本來包含(結果沒有出現任何樣式)。
為了清楚起見,圖片是用戶將看到的一個卡片的放大截圖(因此它應該是樣式化的......而不是在那個實際的視圖中輸出html)
以下是我的設定
actiontext.scss@import "trex/dist/trix"。
.TRIX-content {
.attachment-gallery {
> action-text-attachment,
> .attachment {
flex: 1 0 33%;
padding: 0 0.5em;
max-width: 33%;
}
&. attachment-gallery-2,
&.attachment-gallery-4 {
> action-text-attachment,
> .attachment {
flex-basis: 50%;
max-width: 50%;
}
}
}
action-text-attachment {
.attachment {
padding: 0 !important;
max-width: 100% ! important。
}
}
application.scss
@import "actiontext"。
我的new.html.erb中的樣式可以使用,但發布時卻沒有顯示上述的樣式。
繼續
我的表單。 new.html.erb
<div class="page-content-area">
<h1>創建一個博客帖子</h1>
<h2>填寫以下詳細資訊</h2>
<%= simple_form_for [@blogposts] do|f| %>
<%= f.input :blog_title,
required: true,
autofocus: true,
input_html: { Autocomplete: "Blogpost Title"/span> }%>
<%= f.input :blog_sub_heading,
required: true,
autofocus: true,
input_html: { Autocomplete: "Blogpost Sub Heading"/span> }%>
<%= f.input :blog_text,
as: :rich_text_area,
required: true,
autofocus: true,
input_html: { class: "text-area-blog", autocomplete: "Blog Text" }%>
<%= f.input :photo,
autofocus: true,
as: :file %>
<%= f.submit "CREATE"/span>, class: "btn btn-primary" %>
<%結束%>
</div>
application.js
require("trex")
require("@rails/actiontext"/span>)
blogpost控制器
def new
@blogposts = Blogpost.new
end
def create
@blogposts = Blogpost.new(blogpost_params)
@blogposts.user = current_user
if @blogposts.save!
redirect_to blogpost_path(@blogposts)
else.
呈現'new'。
end
end
私有
def blogpost_params
params.require(:blogpost)。 permit(:user_id, :blog_title, : blog_sub_heading, :blog_text, :photo, :content)
結束。
blogpost模型
class Blogpost < ApplicationRecord
belongs_to:user
has_rich_text :content[/span]。
end
如果你需要我顯示其他東西,請不要猶豫,讓我知道!
感謝您的支持。
謝謝你的時間!
uj5u.com熱心網友回復:
你是如何在你的視圖中渲染blog_text欄位的?一般來說,從用戶內容中渲染HTML是受限制的(以避免對XSS/等等)。你可以使用.html_safe來告訴rails不要轉義html標簽。參見raw vs. html_safe vs. h來取消轉義html
uj5u.com熱心網友回復:
所以我意識到我的錯誤。
代替了
class Blogpost < ApplicationRecord
belongs_to:user
has_rich_text :content[/span]。
end
它應該是
class Blogpost < ApplicationRecord
belongs_to:user
has_rich_text :blog_text 豐富的文本。
結束。
因為在我的應用程式中,我已經在我的模式和表單中把該列命名為 "blog_text"。
我太緊跟教程了,沒有把它與我的模式結合起來。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322769.html
標籤:
上一篇:無法更新Rails。錯誤資訊"無法找到'id'=:id的電影"
下一篇:類的記錄器默認值和子類的新檔案

