Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Angular js provides its own routes. rails routes will always be used when you want to connect your views with database(CRUD). that is data binding with angular.js now i will show you how to use it with rails...</p> <p>I want to add new user using angular js views and list all users. i will use rails as backend to store users in database so i will create user_controller</p> <ol> <li><p>First Create resource of user. we will not create any views so be carefull for that</p> <pre><code>rails g resource user name:string </code></pre></li> <li><p>Then go to route.rb and check there is a route created, and create Home controller with index action that we will use as our single page application where our output will be yield.</p> <pre><code>rails g controller home index root to: "home#index" </code></pre></li> <li><p>type this command on terminal where you will see our 7 routes. we never use new and edit method, because new and edit don't respond to json.</p> <pre><code>rake routes </code></pre></li> <li><p>Now we want all user with a unique name so go to create_user migration and uncomment this line and then we will migrate the database.</p> <pre><code>add_index :users, :name, unique: true rake db:migrate </code></pre></li> <li><p>Now modify our controller so that all data fetching and insertion will be done with json format. so put this code inside users_controller.rb, if you are using rails 4 then follow this otherwise not. attributes will be adjustable in model.</p> <pre><code>class UsersController &lt; ApplicationController respond_to :json def index respond_with User.all end def create respond_with User.create(user_params) end private def user_params params.require(:user).permit(:name) end end </code></pre> <p>Its time to use angulajs in our rails application</p></li> <li><p>Go to application.html.erb replace tag line with this</p> <pre><code> &lt;html ng-app="userApp"&gt; </code></pre></li> <li><p>Now add angular.js file in asses/javascripts, download it from <a href="http://angularjs.org/" rel="noreferrer">http://angularjs.org/</a></p></li> <li><p>Now we will create a controller to handle our angular application routes to know then that this controller wants something from me. so type this on terminal</p> <pre><code>mkdir -p app/assets/javascripts/angular/controllers </code></pre></li> <li><p>Now let's create the controller file itself. I'm calling this controller the "userCtrl", Thus our filename will be app/assets/javascripts/angular/controllers/userCtrl.js.coffee</p></li> <li><p>In controller userCtrl</p> <pre><code>app = angular.module("userApp", ["ngResource"]) @userCtrl = ($scope, $resource) -&gt; User = $resource("/users", {id: "@id"}, {update: {method: "PUT"}}) $scope.users = User.query() $scope.createUser = -&gt; user = User.save($scope.newUser) $scope.users.push(user) </code></pre> <p>(This will insert new user record into database, by creating createUser method)</p></li> <li><p>Create a view for taking input from user write this code in home/index.html.erb.</p> <pre><code>&lt;div ng-controller="userCtrl"&gt; &lt;form ng-submit="createUser()"&gt; &lt;label&gt;Enter Name&lt;/label&gt; &lt;input type="text" placeholder="Enter user name" ng-model="newUser.name" /&gt; &lt;button ng-click="createUser()" class="button tiny"&gt;Submit&lt;/button&gt; &lt;/form&gt; &lt;!-- List all usres --&gt; &lt;h4 ng-show="users.$resolved &amp;&amp; users.length &gt; 1"&gt;All users&lt;/h4&gt; &lt;span ng-repeat="c in users"&gt; {{c.name}}&lt;br&gt; &lt;/span&gt; &lt;/div&gt; </code></pre></li> </ol> <p>Run application and watch output.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload