What is the best way to change actiontext attachments into href url for json format?

for json response actiontext body is converted with .to_html

but the problem with that is the attachment is inside <action-text-attachment> tag

and I find it hard to render it into my mobile app since the library only supports a plain html tag, such as a or href thats why I want to change it all into a href

my questions is

what is the best way to change this tag into a clickable href?

I’m thinking about using nokogiri to find and replace

anyone have better idea?

1 Like

I am not sure if enough people have this issue to warrant something like this, but it could be possible to add a config in an initializer, for what you want TAG_NAME to be here:

This is probably a bad idea but you might be able to monkey-patch the TAG_NAME constant :man_shrugging:. Otherwise parsing and changing using nokogiri works.

I created a PR to add the config so you can do:

config.action_text.attachment_tag_name = "div"
1 Like

This looks like a really clean patch. I am curious, though, what happens to existing documents in the database when this attribute is changed after they are created? One of the issues I have with Trix is that it stores the document in its converted, HTML format. I understand why this is from a performance standpoint, but it still means that there is no way to improve existing documents short of find-n-replace. Trix goes to the trouble of creating its own DOM in memory while editing is underway, but it doesn’t store that object graph anywhere after editing happens. Instead, the resulting HTML is written out to disk.


1 Like

Great question. I think I would leave existing HTML as is. It would be very easy to write a script/migration to change the existing attachment tag name, considering the HTML is stored. Maybe I should write that example script and include in the description of the PR so that anyone wanting to retroactively change the tag name can do it easily.

I just realize we already could change some html results for another component like field error

we could use initializer to config it just like explained in here

the case in link is we change the default field error response field_error_proc from <div class="field_with_errors"></div> into more bootstrap compatible version

I wish we could do the same for actiontext attachments HTML response without using nokogiri to hack the response

The commit was merged.

Once you use a version of Rails with this commit you can change the attachment HTML tag.

holy shits congrats bro :tada:

1 Like