Rails ActionText to_attachable_partial_path 在编辑时被忽略

huangapple go评论63阅读模式
英文:

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

Rails ActionText to_attachable_partial_path 在编辑时被忽略

And again on show pages/page_embed

Rails ActionText to_attachable_partial_path 在编辑时被忽略

But on edit, it shows pages#page

Rails ActionText to_attachable_partial_path 在编辑时被忽略

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

Rails ActionText to_attachable_partial_path 在编辑时被忽略

And again on show pages/page_embed

Rails ActionText to_attachable_partial_path 在编辑时被忽略

But on edit, it shows pages#page

Rails ActionText to_attachable_partial_path 在编辑时被忽略

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:

&gt; Action Text renders embedded `&lt;action-text-attachment&gt;` 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 `&lt;action-text-attachment&gt;` element.

&gt; To render a different partial, define User#to_attachable_partial_path:

Doesn&#39;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&#39;t include rendering the edit field.

</details>



huangapple
  • 本文由 发表于 2023年4月17日 04:03:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定