Hi,
I am trying to figure out how to implement AJAX to set a session
variable when a user selects from a dropdown. I have not used AJAX
before.
My dropdown code is:
<%= collection_select(
:question,
:subject_id,
Subject.order(:title),
:id,
:title,
:prompt => "Select a Subject") %>
My coffeescript is
jQuery ->
books = $('#question_book_id').html()
$('#question_subject_id').change ->
alert("Set the session[subject] now!")
subject = $('#question_subject_id :selected').text()
...
I assume I create a method in users_controller to change the session
variable. My question is how do I call it in the jquery code?
on 2013-02-09 21:39
on 2013-02-10 17:18
This seems to work...
Routes:
match '/users/update_subject', :to => 'users#update_subject', :as =>
:update_subject
jQuery:
...
$.ajax(
url: '/users/update_subject',
data: "subject_title=" + subject)
Controller:
def update_subject
session[:subject] = params[:subject_title]
end
Can anyone tell me if it is OK to use a GET request (default) for this
or do I need to specify a POST, as I am only using this to set a
session variable and not hitting the database?
Thanks!
on 2013-02-26 23:29
Hi.
You can fixed this issue by next way:
Controller:
def update_subject
session[:subject] = params[:subject_title]
* render action: 'ANY_STUB_PAGE' and return *
end
This action updates session value at cookies in client side. Stub page
will
not be visible, because incoming data will not be rendered on the
screen,
only in the backend.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.