Ruby on Rails - Send JavaScript to view

Posted by Eef on Stack Overflow See other posts from Stack Overflow or by Eef
Published on 2010-04-27T14:09:06Z Indexed on 2010/04/27 14:13 UTC
Read the original article Hit count: 222

Filed under:
|

Hey,

I am creating a website in Ruby on Rails. I have a controller action that renders a view like so:

def show
  time_left = Time.now.to_i - 3.hours.to_i
  @character = current_user.characters.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @character }
  end
end

This is fine as it renders the show.html.erb as I like. I would like however to somehow pass time_left to the view as a Javascript variable as this value is use by a countdown JQuery plugin.

I could put a javascript block on the page in the HTML and print a instance variable out like so:

<script type="javascript"> $('#countdown').countdown('<%= @time_left =>')</script>

But I would like to keep all my JS in a external file and off the page could anyone give some advice on how to implement this?

Cheers

Eef

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about ruby-on-rails