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()
    escaped_subject = subject.replace(/([
#;&,.+*~\'':"!^$[\]()=>|\/@])/g, ''\\$1'')
    options
$(books).filter("optgroup[label=''#{escaped_subject}'']").html()
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?
-- 
Posted via http://www.ruby-forum.com/.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
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!
-- 
Posted via http://www.ruby-forum.com/.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
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.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/0ClOu0k3tNAJ.
For more options, visit https://groups.google.com/groups/opt_out.