How to remove redirects in Rails / Angularjs html5 mode

How can I avoid a performance hit every time a user refreshes a page with angled corners when using html 5 mode with rails? I am using Ruby on Rails route redirection in Angularjs html5 mode. With my setup, if I am http://localhost:3000/users, the URL immediately becomes http://localhost:3000/?goto=users, which is then redirected to http://localhost:3000/users. What can I do to avoid this type of redirect when using html5 mode?

Rails Routes

myApp::Application.routes.draw do
 namespace :api, defaults: {format: :json} do
   namespace :v1 do
    resources :users
   end
 end

# Re-route all angular requests to the angular router
get "/*path" => redirect("/?goto=%{path}")
root to: 'application#home'

end

Angular Routes

myApp.config(function($routeProvider, $locationProvider, $httpProvider) {
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = 
    $('meta[name=csrf-token]').attr('content');
    $routeProvider.
        when('/', {
            templateUrl: '../assets/mainIndex.html',            
            controller: 'MainController',
            redirectTo:
                function(current, path, search){
                  if(search.goto){
                    // if we were passed in a search param, and it has a path
                    // to redirect to, then redirect to that path
                    return "/" + search.goto;
                  }
                  else{
                    // else just redirect back to this location
                    // angular is smart enough to only do this once.
                    return "/";
                  }
                }
        }).when('/users', {
            templateUrl: '../assets/users/userIndex.html',          
            controller: 'UsersController'
        }).otherwise({
            redirectTo:'/'
        });
    $locationProvider.html5Mode(true);
});

Above from this blog: http://omarriott.com/aux/angularjs-html5-routing-rails/

+4
source share
2

:

  # Route all valid requests to AngularJS
  root to: 'application#home'
  get "*path" => "application#home"

action_controller:

  # We'll just use this as a launch point for our App
  def home
    # Render just the layout since this application is Angular driven
    # our layout/application has all the angular logic and our controllers
    # have no views for themselves. We just need a place to launch from
    # and this happens to be it. So we have no view (thus :nothing => true)
    # but we still want the layout (since it has the App bootstrap code)
    render :layout => 'application', :nothing => true
  end

angularjs. .

+5

:

# Gemfile
gem 'rack-rewrite'

# config.ru
# This file is used by Rack-based servers to start the application.
use Rack::Rewrite do
  rewrite %r{^(?!.*(api|\.)).*$}, '/index.html'
end

require ::File.expand_path('../config/environment',  __FILE__)
run Rails.application

: https://www.angularonrails.com/get-html5-pushstate-working-angular-rails/

0

Source: https://habr.com/ru/post/1536333/


All Articles