I want use angularjs in a rails project and I'm new to angularjs. for this, I create a staticpage
controller with index
action. I set this page for root page:
config/routes.rb
Restauranteur::Application.routes.draw do
root "staticpage#index"
end
now I download angular.js
and add to asset pipeline. I remove turbolink
and add below code to view/layouts/application.html.erb
:
<!DOCTYPE html>
<html ng-app="restauranteur">
<head>
<title>Restauranteur</title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div ng-view>
<%= yield %>
</div>
</body>
</html>
Then, I create HomeCtrl.js.coffee
:
@restauranteur.controller 'HomeCtrl', ['$scope', ($scope) ->
# Notice how this controller body is empty
]
Now, I set an angular route for show default page, For this I create main.js.coffee
and create a templates
directory in public folder.
main.js.coffee
:
@restauranteur = angular.module('restauranteur', [])
@restauranteur.config(['$routeProvider', ($routeProvider) ->
$routeProvider.
otherwise({
templateUrl: '../templates/home.html',
controller: 'HomeCtrl'
})
])
I create home.html
in public/templates
folder.
I want when I run the server,I see home.html
, but the server run staticpage/index
, where is the problem? Why home.html
isn't set as root page and I cannot preview this?
server log
:
Started GET "/" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Processing by StaticpageController#index as HTML
Rendered staticpage/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 12ms (Views: 11.0ms | ActiveRecord: 0.0ms)
Started GET "/assets/staticpage.css?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/restaurants.css?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/angular.js?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/main.js?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/angular/controllers/HomeCtrl.js?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-07-17 18:54:31 +0430
Note: I use this tutorial for do this, but I am not successful.