Saving hyperlinks in a table

the hyperlink works outside the table…

> transaction.invoice_number = "<a href= '<%=[0].receipt_url %>' >here</a>"

(a link to the url of the invoice as a pdf, it saves the whole string as text), but doesn’t create the link…

Check the “Safe strings” section of the Rails guide, I think it’s what you need.

Something like: <%= raw transaction.invoice_number %>

And a better solution is to store only the URL on the database and build the link the View, so you can change how to render this link easily in the future.

1 Like

I would recommend <%= sanitize transaction.invoice_number %> instead of raw. Rubocop will yell at you for using raw. sanitize will pass back the same HTML and mark it as safe, but after scrubbing it of anything that could possibly get into it and turn it against you.


Ok, thanks. I will give that a try.

I would recommend not storing markup in your DB. If you need to store the URL as a separate data point from your invoice number then create a field for that. So:

transaction.invoice_number = '12345'
transaction.invoice_url =[0].receipt_url

Then in your template create a link with those fields:

<%= link_to transaction.invoice_number, transaction.invoice_url %>
1 Like

ok, thanks I’ll make another column in my table.