How to use scripts in Arbre?

287 views Asked by At

In the docs it says that to put JavaScript into Arbre you must use script { raw ... }. At first I understood from this to write script { raw [insert code here]}. With the word raw as being part of it. That didn't work. So then I thought it might be that it was script {[insert code here]}, but that also didn't work. I am writing the code as a string because if I try to write it directly, Ruby says the code is not ruby code and accuses an error. But when I look into the HTML with Inspect, the JS code appears almost perfectly there, except that wherever I put quotation marks, instead it is written ", and I don't know how to make it so it actually places quotations properly.

Does anyone know what I am doing wrong?

Thanks in advance!

1

There are 1 answers

0
Sjors Branderhorst On

One way:

script { "alert('here we are');".html_safe }

Another way:

show do

  render \
    :partial => 'shared/myjavascript', 
    :locals => { 
      :how_did_we_get_here => 'yeah, also locals!' 
    }

  columns do
    column do
      attributes_table do
      # ....
    end
  end
end

Then in app/views/shared/_myjavascript.html.erb place the following:

<%= javascript_tag do %>
  alert("<%= how_did_we_get_here %>");
<% end %>