admin管理员组

文章数量:1406937

I am working on one script which has been called from the websocket . This page of code is of html.erb

It pass variable to the javascript, and from that javascript variable i want to assign it to ruby variable ,

Here is the code

function set_color(val1,val2)
{

  <%background_color_id = %>
  var obj_color_id = '<%=background_color_id ='+val2+'%>' ;
  console.log(obj_color_id)
 }

The result from console log is +val2+

If I pass var obj_color_id = '<%=background_color_id ='val2'%>' ;

The result from console log is val2

Please help me assign javascript variable to ruby variable

I am working on one script which has been called from the websocket . This page of code is of html.erb

It pass variable to the javascript, and from that javascript variable i want to assign it to ruby variable ,

Here is the code

function set_color(val1,val2)
{

  <%background_color_id = %>
  var obj_color_id = '<%=background_color_id ='+val2+'%>' ;
  console.log(obj_color_id)
 }

The result from console log is +val2+

If I pass var obj_color_id = '<%=background_color_id ='val2'%>' ;

The result from console log is val2

Please help me assign javascript variable to ruby variable

Share Improve this question edited Aug 22, 2012 at 6:26 Arpit Vaishnav asked Aug 22, 2012 at 6:15 Arpit VaishnavArpit Vaishnav 4,7806 gold badges41 silver badges57 bronze badges 1
  • try var obj_color_id = 'background_color_id<%='val2'%>' ; – dnyan waychal Commented Aug 22, 2012 at 6:25
Add a ment  | 

3 Answers 3

Reset to default 3

You cannot do this. Javascript runs Client side, Ruby runs Server side.

You can't do that. All values <%= are translated on server side and their values are send to client. There is no ruby on client side. You have to send request to your websocket or http server in order to pass some data to server.

Actually, if I understand your code (your question is not well phrased, unfortunately), the simple solution is:

1- Assign a value via server-side code:

function set_color(val1,val2)
{
  var bkgdColorId = "<%= background_color_id %>";
  var obj_color_id = bkgdColorId;
  console.log(obj_color_id)
}

2- (Or,) Assign a value from client-side code:

function set_color(val1,val2)
{
  /** pseudo-code **/
  on-click-event: makeAjaxCallToServer(){
    urlForWebService, { color: val2 }
  }
}

Using some jQuery (if assigning to server from client-side event) would greatly facilitate this process.

本文标签: assign javascript variable to ruby variableStack Overflow