1

I know this is possibe inside a js function

<% session[:comment] = "This is the comment" %>

But is there any way to make this work.

var comment = "This is the comment";
<% session[:comment] = comment %>

Because in my app I get this comment from a text field. So i have to set the string dynamically. So is there a way I can make this happen.

2 Answers 2

2

I don't think u can set session in this way.

But a solution will be to use cookies instead of session.

There are JS plugins to set cookies and you can use them in your ruby code.

Sign up to request clarification or add additional context in comments.

Comments

0

This is not possible directly. The javascript part runs on the client side and hence you cant insert the values into a server side variable without sending it back to the server. You will have to use ajax calls to do it.

This quora post should help : http://www.quora.com/Ruby-on-Rails-web-framework/Can-I-pass-a-JavaScript-variable-to-a-Rails-method

5 Comments

So I cant set this session.. So sad. I cant use ajax because as soon as this comment gets set another url is getting caled
You can always place an ajax call before the next url is hit. Need more insight on your code for the exact solution.
actualy there is a text field.. and using jquery onchange im saving its value in comments variable. I have an a tag with onclick im going to another url which is out from the project(fb_share_page). and on closing that page fb automatically gives a '/fb_close' url. and with which I moves to another controller method.
I want this comment in that controller. As i cant set any params in fb_close.. i want to set session.
Before the redirection (inside the on change itself it can be done judging by the looks of it) you need to make an ajax call and store it in a persistent variable(maybe a static variable or global constant or DB). After this ajax call completes you can proceed with the other options inside the success block for the ajax call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.