Rails: passing data to the browser with <meta>
Alex Antsiferov

Alex Antsiferov @wetterkrank

About: random rants and code snippets

Location:
Berlin, Germany
Joined:
Jan 13, 2021

Rails: passing data to the browser with <meta>

Publish Date: Jan 5
0 0

I'll be posting code snippets like this -- solutions to little problems that I come across in everyday work.

This one is one of the simplest ways to share some structured data with the front end using content_for helper and meta element.

# .erb

<% content_for :head do %>
  <meta name="project-prices" content=<%=prices.to_json%> />
<% end %>
Enter fullscreen mode Exit fullscreen mode
// .js

const meta = document.querySelector('[name="project-prices"]')
JSON.parse(meta.content)

Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment