Sending parameters from controller to view

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"}

but it doesn't work... Do you know how to do that ?

Thanks

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.

Colin

def show_one_publisher_seven_days
@title = 'foo'
end

...

%script{:type => "text/javascript"}

   var title\_text = &lt;% @title %&gt;       &lt;&lt;&lt;===============

Can you tell us *how* it's not working, i.e., what it does instead?
Are you getting some kind of Javascript error?

I don't know haml yet, but it looks to me like that would probably
result in saying:

        var title_text = foo

whereas you want::

        var title_text = 'foo';

Try adding the quotes and semicolon and see what happens. If that
doesn't fix it, see if you're getting a JS error, and if so, tell us
what it is.

-Dave

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 = &lt;% @title %&gt;       &lt;&lt;&lt;===============

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}'"

Colin

Just to clarify this, I think the = "... syntax will work. I have not
seen the == syntax previously, it does work, but I cannot see it
documented at http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html

Am I just missing it there?

Colin

Thanks a lot Colin !!!
With => = "var title_text = '#{@title}'"
It works perfectly !