0

On one projects#index page, I've 3 different views [Grid, Map, Stats] and each view has a button. When user clicks any view it sends a AJAX Request and load the required content for that view.By default the Grid view gets loaded.

now my client wants to directly load the view based of following URL:

 www.somedomain.com/projects#grid
 www.somedomain.com/projects#stats
 www.somedomain.com/projects#map

But when i check the controller, it doesnt get the information of #grid,#stats and #maps. How we can achieve this in rails. is this possible ? He doesnt want to go with:

 www.somedomain.com/projects?view=grid
 www.somedomain.com/projects?view=stats
 www.somedomain.com/projects?view=maps
kashif
  • 1,097
  • 4
  • 17
  • 32

1 Answers1

0

I'm afraid it is not possible. To your backend application, only the part www.somedomain.com/projects from whole www.somedomain.com/projects#grid is sent. "Things" after # are meant to be for browsers.

Said so - it is meant for web browser - check this answer how you can "grab" the hash from url with javascript.

Here is an explanation how you can "get" and "set" values in hash from url - if you would like to manipulate it from your application.

Just to summarise:

var myCurrentLocation = window.location.hash.substr(1);

switch (myCurrentLocation) {
    case 'stats':
        $.ajax(/** handler for stats */);
        break;
    case 'map':
        $.ajax(/** handler for map */);
        break;
    default:
        $.ajax(/** handler for grid, and default */);
        break;
}


/*
 * Other code
 */

$('#showStats').click(function () {
    location.hash = '#stats';
});

Good luck!

Community
  • 1
  • 1
Paweł Dawczak
  • 9,519
  • 2
  • 24
  • 37
  • thanks for your quick feedback but if we go with Javascipt. it means 2 requests if user have # in the URL. first it will load the default content and then JS will check the # view. and send the request again to render that specific view. right ? – kashif Mar 19 '15 at 06:49
  • @Kashif - It depends on how your current application works, but it is possible. Well - as mentioned in answer - you can't send url hash to server. It is just impossible. You can interact with value stored in the hash only in client (web browser). Please, check my answer updated with some conception how you can proceed. – Paweł Dawczak Mar 19 '15 at 06:57