I am grateful to this forum for helping me out yesterday and I'm wondering if you might get me over another hump today. I come from a .NET background and I can't seem to get the Rails way to do something that seems like it should be simple. I have read through so many posts about topics which almost address this, but nothing quite gets me all the way there. I would really appreciate your expertise.

This is a student enrollment app with students, classes, enrollments, etc.

I am storing a term_id value in the session. It needs to be available to all pages, since most things are viewed by the term. I need each page to have a header with a dropdown menu, so that the user can change the term at any time (these are week-long terms : week 1, week 2, etc). This will display enrollments or classes, etc. filtered for the currently selected term.

In .NET I might have a code-behind event (like term_selectChanged) in which I would set the session value to the selected value. I am guessing that there is a Rails way which is just as simple, but I can't seem to find it. Even the book "Rails for .NET Developers" doesn't seem to talk about this. I have successfully implemented collection_select for existing forms, and have tried modifying various versions of select and form helpers, but my head is spinning and I am getting nowhere. I am loving Rails so I think there must be an easy way that is right under my nose.

I have also created a user_session model class (without a backing table) as suggested by Ryan Bates (http://railscasts.co...ion-based-model), which allows me to wrap the session in a helper method. I think I understand how that works, but I am not sure it helps me in any way with this.

I would really appreciate some guidance. I am open to not using session if there is a better way to have a variable available to all pages.

You'll have to use that helper to access the session information and make it available for your views/controllers to display the proper information.

I am assuming that the helper method is made available to all your controller via the application controller

For example

class SomeController < ApplicationController
def some_method
@value = session_helper_returned_value # set the value from the session to an instance variable and now the view can parse this information.
end
end

I am familiar with how to get and set the session, I was just hoping that there was a simple way to do it in a select control. If you say ajax is the best way, I will try that. I was thinking that this was such a common thing to do that there might be a simpler way. Perhaps when I work out the ajax code, I will realize that it is in fact a simple way....

You'll have to use that helper to access the session information and make it available for your views/controllers to display the proper information.

I am assuming that the helper method is made available to all your controller via the application controller

For example

class SomeController < ApplicationController
def some_method
@value = session_helper_returned_value # set the value from the session to an instance variable and now the view can parse this information.
end
end

I like where you're going with this. I'll see if I can make it work. If not ....ajax.

You can always run rake routes to get a view at all the routes in your app.

Rails make the routes pretty all by itself. Your

resources :sections do
member do
get :set_user_term
end
end

instructs Rails to make a resource named sections with a member method named set_user_term. Rails thus creates all sorts of routes for you. If we ran rake routes it would probably give us something like:

What this tells us is that Rails has created all the default CRUD (Create, Read, Update, Delete) stuff for us. Lets look at the from the bottom up.

The destroy and update methods have been given no name (the column at the far left), however, we can see that it is the same route (the /sections/:id(.:format) stuff) as the one named section. The :id here tells us that this is a member method, it needs an id to function. The HTTP-action (GET, PUT, POST, DELETE) used for the request, tells Rails which method to execute (the far right column).

Looking at your URL, with set_user_term, Rails want this to be /sections/:id/set_user_term(.:format). So in other for you to test it in your browser, you'll need to direct it to

http://localhost:3000/sections/2/set_user_term

This is also why you get the error you get. Rails thinks that the set_user_term part in your URL is the id for the section.

That was very helpful. I am reading more about routes and trying to understand them better. My problem is that I don't want to call a method for a particular section, In fact, I chose the Section controller arbitrarily, since the method is not related to any existing controllers (it is merely setting a value in the session). But I thought that I needed to put it on a controller, so I chose Section, which seemed most appropriate. But I don't want to call the method for a particular section, so I created a class method on the controller. Perhaps I should find a better place to put the method?

I suspect the way I am doing it is not normal and perhaps not recommended. In the Rails guides under "Singular Resource" I found this which sounds somewhat similar to what I need, but I haven't got it working yet:

"Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action:

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.

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.