Load only products based on region ideas please :)
Hi all,
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
site.com/region/overview
site.com/region/products
site.com/region/:region_id/overview
site.com/region/:region_id/products
Idea 2: Set a cookie, and load based on that?
Hey Alan!
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 site.com/texas/13/products
.
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.
Cheers! :)
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 params.key?(:region_id)
before current_user.region_id
and cookies.key?(:region_id)
so if the user is signed in and receives a link from someone else the proper results will display.