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?
-
2I 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."Jordan Running– Jordan Running2015年07月04日 20:43:17 +00:00Commented Jul 4, 2015 at 20:43
-
Yes, aware. My mistake. Nice quote.Cameron Aziz– Cameron Aziz2015年07月04日 20:44:00 +00:00Commented Jul 4, 2015 at 20:44
1 Answer 1
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