Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Access Ruby Session Variable in Javascript

I have created Ruby Session Variable I need to access this variable in Javascript

Lets take this scenario

session[:note] = 'Some Notes' this is done in Ruby on Rails

Now I want to access this is like

var session_val = js-code for access above session variable.

its not js.erb I know if it is js.erb we can access like

var sesion_val = "<%= session[:note] %>"

TIA

like image 914
Veeru Avatar asked Apr 22 '15 07:04

Veeru


1 Answers

You can use HTML5 data method from where you can access the variable.

suppose, your JS function is calling from a button. thus, you can define that button as:

<button class="btn" data-session="<%= session[:note]%>" onclick="seesionCheck()">Hello</button>

now, in your JS function you can access the variable as:

$(".btn").data('session');
like image 163
Emu Avatar answered Sep 21 '22 06:09

Emu