Hi all,
I want to create charts in my website. I want to fill them with
variables. So in my controller, I put the variables I want to use :
class ChartsController < ApplicationController
def show_one_publisher_seven_days @title = ‘foo’
end
end
And in my view (show_one_publisher_seven_days.haml), I want to assign
my javascript variable with the variable I created in my controller.
%script{:type => “text/javascript”}
var title_text = <% @title %> <<<===============
%p This is the chart for one publisher for the last seven days
%div{:id => “7days_one_publisher”, :style => “width: 800px; height:
400px; margin: 0 auto”}
And in my view (show_one_publisher_seven_days.haml), I want to assign
my javascript variable with the variable I created in my controller.
%script{:type => “text/javascript”}
var title_text = <% @title %> <<<===============
It is haml not erb. Try
= “var title_text = #{@title}”
Check the html generated in the browser to check it is what you expect.
By the way, it is generally considered bad form to put javascript like
this in the view. Ideally all javascript should be in separate files.
It is haml not erb. Try
= “var title_text = #{@title}”
Oops, quickly returning to add to this after seeing Dave’s response
= “var title_text = ‘#{@title}’”
That should be
== var title_text = ‘#{@title}’
(two equals signs at the beginning and no outer quotes)
But this is a terrible idea. What you want to do instead is put the
value in a hidden div, then have your JS look at that div. You should
never have to generate JS dynamically from Ruby.