Visitar URL original
Adds locals when rendering ActionText attachments by excid3 · Pull Request #58511 · rails/rails · GitHub
Skip to content

Adds locals when rendering ActionText attachments - #58511

Open
excid3 wants to merge 2 commits into
rails:mainfrom
excid3:action-text-locals
Open

excid3 wants to merge 2 commits into
rails:mainfrom
excid3:action-text-locals

Conversation

@excid3

@excid3 excid3 commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Motivation / Background

ActionText attachments may need additional context from where they're rendered. Right now, there is no way to reference the context which the ActionText::Content is rendered in so it can't really be dynamic.

I ran into this initially rendering ActionText::Content in an admin area where I wanted attachments to render different URLs than the customer facing version.

Another case was rendering some ActionText::Content inside and outside Turbo Frames. When inside, I need to ensure the links in ActionText are rendered with data-turbo-frame="_top" and I really need the context to be passed in.

Detail

Allows users to render ActionText::Content with locals passed along to attachments.

<%= render post.body, locals: {turbo_frame: :_top} %>

These locals are passed along to attachment partials.

@jorgemanrubia would you find this helpful?

Additional Information

I also added a default local in_editor: true for to_editor_html like in_gallery: true for images.

It's common practice to use the same partial for both the regular and editor HTML. You could use this to make links disabled in the editor (a user accidentally clicking a link in the editor could lose their writing) for example:

<%= link_to_if !local_assigns[:in_editor], post.title, post %>

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@seanpdoyle seanpdoyle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you expand this diff to also include some test coverage, as well as an entry in Action Text's CHANGELOG.md?

Comment thread actiontext/app/helpers/action_text/content_helper.rb Outdated
Comment thread actiontext/app/helpers/action_text/content_helper.rb
@excid3
excid3 force-pushed the action-text-locals branch from a85f8bc to bc0372a Compare August 24, 2026 18:09
@excid3

excid3 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

@seanpdoyle refactored this to use render_in 👍

Comment thread actiontext/lib/action_text/content.rb Outdated

@jorgemanrubia jorgemanrubia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jeremy could we merge this one please?

Introduces `render_rich_text` to pass local variables to ActionText attachment partials.

```erb
<%= render_rich_text post.body, locals: {turbo_frame: :_top} %>
```
By adding `render_in` support to ActionText::RichText and
ActionText::Content, users can render directly in the view with locals:

    <%= render @post.body, turbo_frame: :_top %>

Attachment partials can then read them from `local_assigns`:

    <%# app/views/users/_user.html.erb %>
    <%= link_to user.name, user, data: { turbo_frame: local_assigns[:turbo_frame] } %>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants