使用rails重定向到html5mode中的角度路径

时间:2016-05-09 04:16:26

标签: javascript ruby-on-rails angularjs ruby-on-rails-4 angular-ui-router

我使用'#'删除了angular中的html5Mode并且它可以正常工作但是当我刷新它在rails中寻找模板而不是角度并且抛出模板未找到错误时。

角度路线

app.config([
    "$stateProvider", "$urlRouterProvider","$locationProvider",
    function ($stateProvider, $urlRouterProvider, $locationProvider) {
        $stateProvider
            .state("home", {
                url: "/",
                controller: "homeCtrl",
                templateUrl: "angular/templates/home.html"
            })
            .state("/dashboard", {
                url: "/dashboard",
                templateUrl: "angular/templates/dashboard/dashboard.html"
            })
            .state("/dashboard.active", {
                url: "/active",
                templateUrl: "angular/templates/dashboard/active.html",
                controller: "activeCtrl"
            })
            .state("/dashboard.inactive", {
                url: "/inactive",
                templateUrl: "angular/templates/dashboard/inactive.html",
                controller: "inactiveCtrl"
            })
            .state("/dashboard.drafts", {
                url: "/drafts",
                templateUrl: "angular/templates/dashboard/drafts.html",
                controller: "draftsCtrl",
                resolve: {
                    drafts: function (DashboardFactory) {
                        return DashboardFactory.drafts();
                    }
                }
            })
            .state("/room", {
                url: "/rooms/:id",
                templateUrl: "angular/templates/rooms/show.html",
                controller: "roomsCtrl",
                resolve: {
                    room: function (RoomFactory, $stateParams) {
                        var roomId = $stateParams.id;
                        return RoomFactory.show(roomId);
                    }
                }
            })
            .state("/search", {
                url: "/search?:latitude&:longitude",
                templateUrl: "angular/templates/search.html",
                controller: "searchCtrl",
                resolve: {
                    search: function (SearchFactory, $stateParams) {
                        var latitude = $stateParams.latitude;
                        var longitude = $stateParams.longitude;
                        return SearchFactory.search(latitude, longitude);
                    }
                }
            })
            .state("/conversations", {
                url: "/conversations",
                templateUrl: "angular/templates/conversations/conversations.html",
                controller: "conversationsCtrl",
                resolve: {
                    conversations: function (ConversationFactory) {
                        return ConversationFactory.showConversations();
                    }
                }
            })
            .state("/messages", {
                url: "/conversations/:id/messages",
                templateUrl: "angular/templates/conversations/messages.html",
                controller: "messagesCtrl",
                resolve: {
                    conversations: function (ConversationFactory, $stateParams) {
                        var conversationId = $stateParams.id;
                        return ConversationFactory.showMessages(conversationId);
                    }
                }
            })
            .state("/notifications", {
                url: "/notifications",
                templateUrl: "angular/templates/notifications/notifications.html",
                controller: "notificationsCtrl",
                resolve: {
                    notifications: function (NotificationFactory) {
                        return NotificationFactory.getNotifications();
                    }
                }
            });

        // use the HTML5 History API
        $locationProvider.html5Mode(true);
    }
]);  

Rails路线

Rails.application.routes.draw do
  require 'sidekiq/web'
  mount Sidekiq::Web => '/sidekiq'

  devise_for :users,
             :path => '',
             :path_names => {:edit => 'profile'},
             :controllers => {:registrations => 'registrations'}
  resource :pages, only: [:edit] do
    collection do
      patch 'update_password'
    end
  end

  resources :conversations, only: [:create, :index] do
    resources :messages, only: [:create, :index]
  end

  resources :notifications do
    collection do
      post :mark_as_read
    end
  end


  root 'pages#home'
  get '/general' => 'pages#general'
  get '/drafts' => 'rooms#drafts'
  get '/active' => 'rooms#active_listings'
  get '/inactive' => 'rooms#inactive_listings'
  get '/search' => 'pages#search'
  get '/change_password' => 'pages#change_password'



  resources :rooms do
    post :make_active
    post :make_inactive
    resources :photos, only: [:index, :create, :destroy]
  end

  mount ImageUploader.direct_endpoint, at: "/attachments/images"

end

同样在application.html.erb我已将基数设为<base href="/"> ..

如果路径页面(localhost:3000)页面刷新有效..但如果它的搜索页面http://localhost:3000/search或任何其他页面不起作用。

但是当不使用html5mode时,一切正常。我在这里做错了什么?这里有正确的方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以按如下方式禁用requireBase属性。

 $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
  });

请指定$ routeProvider.otherwise(“home”)。也。它应该工作。