Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to set session value in a select drop-down?

session select

  • Please log in to reply
25 replies to this topic

#21 Ohm

Ohm

    Guard

  • Members
  • 181 posts
  • LocationCopenhagen

Posted 19 August 2013 - 08:21 PM

 

Thank you Ohm,

 

The termid=2 is the selection from the drop down select menu, which is used to filter the enrollments to the selected term. I am passing that value via ajax to the server to set the session value session[:termid]. Then each page checks that in a before filter.

 

I created a sessions controller like this:

class SessionsController < ApplicationController  
  def self.set_user_term(termid)    
    user_session.set_term(termid)
  end
end
   I put this line at the top of the routes.rb file:
get 'set_user_term', to: 'sessions#set_user_term'

When I put this in the browser:

http://localhost:3000/set_user_term?termid=2 

        I get :

 
Unknown action
The action 'set_user_term' could not be found for SessionsController

 

 

Almost had it. Your set_user_term shouldn't be a class method. And like Jemagee says, you'll have to have a view, or render nothing like so:

class SessionsController < ApplicationController  
  def set_user_term(termid)    
    user_session.set_term termid
    render :nothing => true
  end
end

If you want a JSON ok back, you could render that with

render :json => :ok

Blog: http://ohm.sh | Twitter: madsohm


#22 priorcode

priorcode

    Signalman

  • Members
  • 19 posts
  • LocationCalifornia

Posted 19 August 2013 - 08:44 PM

Jemagee,

 

I tried creating a view called set_user_term.html.erb and it does appear when I call it in the browser with:

http://localhost:3000/set_user_term?termid=2 

, but the function logic still does not run. I know I must be breaking a fundamental rule somewhere about how to make a controller method, or something....



#23 Ohm

Ohm

    Guard

  • Members
  • 181 posts
  • LocationCopenhagen

Posted 19 August 2013 - 08:54 PM

Does your user_session object actually have a set_term method?

Or is this suppose to be Rails' session? If so, you'll need session, which is a hash.

 

You can set things in session with 

session[:set_term] = termid

and you can unset it again with either

session[:set_term] = nil

or

session.delete(:set_term)

Blog: http://ohm.sh | Twitter: madsohm


#24 priorcode

priorcode

    Signalman

  • Members
  • 19 posts
  • LocationCalifornia

Posted 19 August 2013 - 11:01 PM

Yes, Ohm,  this a helper method shown in a RailsCasts video. It's a simple model class :

class UserSession
   
  def initialize(session, schoolid, userid)
    @session = session
    @session[:termid] ||= 2 #testing
    @session[:schoolid] ||= schoolid 
    @session[:userid] ||= usersid     
  end
  def set_term(termid)
    @session[:termid] = termid
  end
  def current_term
    @session[:termid].to_i
  end  
end

but my problem seems more fundamental. The automatic Rails scaffolding and table relationships are all working so nicely, but i can't seem to figure out how to write a one-line method and then call it. It is either in the wrong place, or not found, or not correct syntax.

 

If we were to forget completely about all of my non-working code, how would you set a session value from a drop down menu?



#25 priorcode

priorcode

    Signalman

  • Members
  • 19 posts
  • LocationCalifornia

Posted 20 August 2013 - 04:21 PM

Success!

Thank you all for your patience and help. You all had good ideas and the bugs were just details I wasn't very good and debugging. Here is the code that finally worked:

 

the script:

jQuery(document).ready(function($){
   $('#term_id').change(function() {     
    var selectedid = $(this).val()    
    $.ajax({
      url: "/sessions/set_user_term",
      data: {curtermid: selectedid},     
      error: function (jqXHR, textStatus, errorThrown) {
        alert(jqXHR, textStatus, errorThrown); 
      },
      success: function() {      
      location.reload();     
      }
    });        
  });
});

The controller/method:

class SessionsController < ApplicationController  
  def set_user_term   
    session[:termid] = params[:curtermid]    
    redirect_to root_path
   end
 end

and the route:

resources :sessions do    
    collection do
     get :set_user_term    
    end    
end 

I can only select one answer as "best answer", but thanks to all!


 

 



#26 Ohm

Ohm

    Guard

  • Members
  • 181 posts
  • LocationCopenhagen

Posted 20 August 2013 - 04:39 PM

You're so very welcome. I didn't see your second to last post before now, but I would have done it in a similar way.

 

The error part of the ajax call should do something useful or not be there at all.

 

The redirect_to_ root_path part of the controller method is being run, however it is the ajax call that is being redirected, which really does nothing. Please consider using render :nothing => true instead.


Blog: http://ohm.sh | Twitter: madsohm





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users