Im trying to work out the best way to do a task.
On the users menu, they are able to select a region they are a member of. Once a user selects a region I want to load all data on the page based on that data until they select another, or reset it to all.
So if a user has not selected an option they will see all data for the regions they are a member of.
I have a couple of ideas, but is there a better way to do this?
Idea 1: have a :region_id in the URL - if none loads all. Other wise have a route like
Idea 2: Set a cookie, and load based on that?
Does SEO matter at all here? If so then you could utilize slugs for the
:region_id so it's
site.com/texas/austin/products instead of
A cookie would be useful if you want there to be any sort of persistence between sessions or between page navigations. So you could do a few checks
region_id = cookies.key?(:region_id) ? cookies[:region_id] : params[:region_id] region_id.present? ? Region.find_by_region_id(region_id) : Region.all
If this is after a user signs in, then you could also persist their region selection in the user object and check for that as well... just be sure you put things in order of precedence
region_id = current_user.region_id.present? ? current_user.region_id : cookies.key?(:region_id) ? cookies[:region_id] : params[:region_id] region_id.present? ? Region.find_by_region_id(region_id) : Region.all
Then if you want to reset it to a default value after they sign out then just be sure to reset it during the destroy session method.
Yeah this is what i was thinking. Cookies were my preffered way, it also means i can keep the url simple.
Thanks as always Jacob. Its good to get feedback on the ideas in my mind :)
Aye, I agree - if SEO makes no difference then keep the URL's simple.
I suppose the only other thing to consider is could the URL's be sharable? If that matters at all then you may consider using both cookies and params so if a user shares the URL it will present the correct info.
This is where you may want to rearrange the ternary operation and put
cookies.key?(:region_id) so if the user is signed in and receives a link from someone else the proper results will display.
Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.