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

On 20 July 2010 20:51, Johann [email protected] wrote:

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.

Colin

On Tue, Jul 20, 2010 at 15:51, Johann [email protected] wrote:

def show_one_publisher_seven_days
@title = ‘foo’
end

%script{:type => “text/javascript”}

   var title_text = <% @title %>       <<<===============

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


Specialization is for insects. | Professional: http://davearonson.com
-Robert Anson Heinlein | Programming: http://codosaur.us
-------------------------------+ Leadership: http://dare2xl.com
Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net

Colin L. wrote:

On 20 July 2010 21:10, Colin L. [email protected] wrote:

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

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.

Colin

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On 20 July 2010 21:25, Marnen Laibow-Koser [email protected] wrote:

Colin L. wrote:


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)

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 !

On 20 July 2010 21:10, Colin L. [email protected] wrote:

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

Oops, quickly returning to add to this after seeing Dave’s response
= “var title_text = ‘#{@title}’”

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs