Passing Ruby variable to javascript for post -


i have h variable in view , h variable:

[{"folder"=>"high risk", "weight"=>"38.8", "stocks"=>[{"id"=>"id3", "name"=>"indian oil corporation ltd.", "weight"=>"14.4"}, {"id"=>"id5", "name"=>"atul auto ltd.", "weight"=>"13.7"}, {"id"=>"id6", "name"=>"hindustan zinc ltd.", "weight"=>"10.6"}]}, {"folder"=>"expecting pe ratio", "weight"=>"42.3", "stocks"=>[{"id"=>"id7", "name"=>"infosys ltd.", "weight"=>"42.3"}]}, {"folder"=>"from kite", "weight"=>"12.8", "stocks"=>[{"id"=>"id8", "name"=>"hindustan unilever ltd.", "weight"=>"12.8"}]}, {"folder"=>"low margin", "weight"=>"6.1", "stocks"=>[{"id"=>"id9", "name"=>"jindal steel & power ltd.", "weight"=>"6.1"}]}] 

this script inside view.erb

<script> if (top.location != location) {        top.location.href = document.location.href ;     } ///some function giving 'val' value      jquery.ajax({      data: 'val=' + total_span,      datatype: 'script',      type: 'post',      url: "/portfolio/slide_change"      });      </script> 

i have 2 doubt have:

1)how , should declare new variable in javascript h value ruby code in file?(way pass h ruby javascript)

2)i wanted post variable backhand 'val' how can that? possible post 2 variable @ time or need concatenate h 'val'?

you can use ruby variables in erb. assuming have variable @stocks available in erb, can output json representation using <%= @stocks.to_json %>. in view.erb can write:

<script> if (top.location != location) {        top.location.href = document.location.href ;     }       var val = <%= h.to_json %>; //rendered view have actual array there.      jquery.ajax({      data: 'val=' + total_span,      datatype: 'script',      type: 'post',      url: "/portfolio/slide_change"      });      </script> 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -