英文:
Rails ActionText to_attachable_partial_path ignored on edit
问题
I have my models setup like this, making page override the default partial path
class Post < ApplicationRecord
has_rich_text :body
end
class Page < ApplicationRecord
include ActionText::Attachable
def to_attachable_partial_path
"pages/page_embed"
end
On creating a new post, the attachment displays the correct partial pages/page_embed
And again on show pages/page_embed
But on edit, it shows pages#page
My _form code for that field is
<%= form.rich_text_area :body, rows: 4, data: { target: "attachments.editor" } %>
anyone any ideas?
英文:
I have my models setup like this, making page override the default partial path
class Post < ApplicationRecord
has_rich_text :body
end
class Page < ApplicationRecord
include ActionText::Attachable
def to_attachable_partial_path
"pages/page_embed"
end
On creating a new post, the attachment displays the correct partial pages/page_embed
And again on show pages/page_embed
But on edit, it shows pages#page
My _form code for that field is
<%= form.rich_text_area :body, rows: 4, data: { target: "attachments.editor" } %>
anyone any ideas?
答案1
得分: 1
class Page < ApplicationRecord
include ActionText::Attachable
def to_attachable_partial_path
"pages/page_embed"
end
# this
def to_trix_content_attachment_partial_path
to_attachable_partial_path
end
end
https://github.com/rails/rails/blob/v7.0.4.3/actiontext/lib/action_text/attachable.rb#L70
我认为,也许在引入 to_attachable_partial_path
时,这个部分被忽略了。文档说:
> Action Text 通过将嵌入的 <action-text-attachment>
元素的 sgid
属性解析为一个实例来呈现它们。一旦解析完成,该实例将传递给 render
。生成的 HTML 作为 <action-text-attachment>
元素的子代嵌入其中。
> 要呈现不同的部分,请定义 User#to_attachable_partial_path:
并没有真正说明任何关于编辑的内容。如果删除默认的 users/_user.html.erb
,编辑页面会出现问题。我想ActiveStorage 图像可以正常呈现,它们是怎么做到的:它们忽略了那个方法。我猜当他们说 render
时,它并不包括渲染编辑字段。
<details>
<summary>英文:</summary>
class Page < ApplicationRecord
include ActionText::Attachable
def to_attachable_partial_path
"pages/page_embed"
end
this
def to_trix_content_attachment_partial_path
to_attachable_partial_path
end
end
*https://github.com/rails/rails/blob/v7.0.4.3/actiontext/lib/action_text/attachable.rb#L70*
I think, maybe this was overlooked when `to_attachable_partial_path` was [introduced](https://github.com/rails/rails/pull/40308). The docs say:
> Action Text renders embedded `<action-text-attachment>` elements by resolving their `sgid` attribute into an instance. Once resolved, that instance is passed along to `render`. The resulting HTML is embedded as a descendant of the `<action-text-attachment>` element.
> To render a different partial, define User#to_attachable_partial_path:
Doesn't really say anything about editing. And if you remove the default `users/_user.html.erb` the edit page breaks. I was thinking *ActiveStorage* images render no problem, what do they do: they [ignore](https://github.com/rails/rails/blob/v7.0.4.3/actiontext/lib/action_text/engine.rb#L48) that method. I guess when they say `render` it doesn't include rendering the edit field.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论