I'm currently learning Rails, and I'm wanting to use angular in my project.
Here's a simple application from scratch.
1). Create a new rails app:
rails new hello_rails
2). Add angular gem to Gemfile
gem 'angularjs-rails'
3). Install the bundle
bundle install
4). Add angular to javascript manifest in app/assets/javascripts/application.js
//=require angular
5). Generate welcome index
rails generate controller welcome index
6). Populate index.html.erb with an angular hello world
<div style = "background-color: grey">
this is index.html.erb<br>
<div ng-app="">
<p>Name : <input type="text" ng-model="name"></p>
<h1>Hello {{name}}</h1>
</div>
</div>
7). Also modify application.html.erb
<pre>
<!DOCTYPE html>
<html>
<head>
<title>HelloRails</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body style = "background-color:green">
This is application.html.erb <br>
<%= link_to "click here to go home", root_path %> <br>
A yeild statement is below: <br>
<%= yield %>
</body>
</html>
8). Set root route to welcome#index
in config/routes.rb
root 'welcome#index'
Run this - this works fine.
Here's what we get:
The angular is working:
However, if I click the link to return to root_path
it stops working
Additionally, if we add some angular to our application.html.erb
the yielded angular stops working.
<div ng-app="">
<p>Name : <input type="text" ng-model="home_name"></p>
<h1>Hello {{home_name}} at home</h1>
</div>
Can you explain why rails is working this way?