1

I am new to Rails. I have calculated some value in a javascript based upon user inputs and now I want to set a variable present in controller function to the calculate value. Is it possible to do so or is there a better approach?

2
  • You can do it if you pass the javascript variable as a parameter to your controller and set the variable Commented Apr 22, 2014 at 4:03
  • You can use "view helpers" for this approach.. Commented Apr 22, 2014 at 6:35

2 Answers 2

2

Are you sending a param with the javascript? Then just set:

@variable = params[:calculate]

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

Comments

0

Set the value into a hidden_field_tag in side the form,then you can get the value in params.

Ex.

In js script

document.getElementsByName('js_hidden').value = 1111;

then the value will assign into the hidden_field_tag

like

hidden_field_tag 'from_js',1111,:id=>"js_hidden"

then

in your controller you can get params[:from_js].

More details click here

Comments

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.