1

I have a simple input text field:

<input type="text" id="master_password" size="20" placeholder="Master Password" />
<a class="btn btn-default" id="master_submit" href="#">Submit</a>

And some javascript listening:

$(document).ready(function() {
  $('#master_submit').click(function() {
    alert("sometext");
  });
});

The alert works, obviously. I want to store the text field (#master_password) in session[:master_pass] as I will be using it to decrypt many passwords stored in the database. I'm pretty sure I have to use some AJAX, but not familiar with it at all. What code would I replace the alert with in the js file (or view, or controller, of course) to store the data as a Ruby variable?

2
  • 2
    I have edited your question to say "JavaScript" instead of "Java." Java is a completely different programming language and the names cannot be used interchangeably. As a cleverer person than me once said, "Java is to JavaScript as ham is to hamster." Commented Jul 4, 2015 at 20:43
  • Yes, aware. My mistake. Nice quote. Commented Jul 4, 2015 at 20:44

1 Answer 1

3

Assuming you're using Rails, you could use javascript to make an AJAX request to the Rails app, and then in Rails, you could set the session value.

In Javascript (jQuery):

var data = "password=" + encodeURIComponent($('#master_password').val());

$.ajax({
  url: '/my_controller/action',
  data: data,
  type: 'post'
})
.done(function(response) {
  // Do something with the response
})
.fail(function(error) {
  // Do something with the error
});

And in Rails, setup a controller with the appropriate route, and in the action:

class MyController < ApplicationController
  ...
  def action # << name whatever you like
    session[:password] = params[:password]
  end
  ...
end
Sign up to request clarification or add additional context in comments.

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.