How to integrate Rails views with angularjs ng-view?

I created a rail view (ERB). However, I want to integrate it into angularjs $routeProvider , but I don’t know which url should fill in templateUrl to give me the corresponding rail view. For example, I tried:

 $routeProvider.when('/pages/foo', { templateUrl: "/pages/foo", controller: "PageFoo" }) 

But the result appears as a blank page. Are these two functions even integrable? Or I need to create new .html files, instead of using the existing .html.erb

+6
source share
2 answers

Angular js provides its own routes. route routes will always be used when you want to connect your views to a database (CRUD). this is data binding using angular.js, now i will show you how to use it with rails ...

I want to add a new user using angular js views and a list of all users. I will use rails as a backend to store users in the database, so I will create user_controller

  • First create a user resource. we will not create any views, so be careful for this

     rails g resource user name:string 
  • Then go to route.rb and check if the created route exists, and create a Home controller with an index action, which we will use as our application for one page where our output will work.

     rails g controller home index root to: "home#index" 
  • enter this command on the terminal where you will see our 7 routes. we never use a new and editable method because new and edit do not respond to json.

     rake routes 
  • Now we want all users with a unique name go to create_user migration and uncomment this line, and then we transfer the database.

     add_index :users, :name, unique: true rake db:migrate 
  • Now change our controller so that all fetching and data insertion are done using json format. so put this code inside users_controller.rb if you are using rails 4 then follow this otherwise. attributes will be adjusted in the model.

     class UsersController < 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 

    Angulajs usage time in our rails app

  • Go to application.html.erb replace the tag string with this

      <html ng-app="userApp"> 
  • Now add the angular.js file to asses / javascripts, download it from http://angularjs.org/

  • Now we will create a controller to handle our angular application routes to know that this controller wants something from me. so enter this on the terminal

     mkdir -p app/assets/javascripts/angular/controllers 
  • Now create the controller file itself. I call this controller "userCtrl". So our name will be Application / Assets / JavaScripts / angular / controllers / userCtrl.js.coffee

  • In userCtrl controller

     app = angular.module("userApp", ["ngResource"]) @userCtrl = ($scope, $resource) -> User = $resource("/users", {id: "@id"}, {update: {method: "PUT"}}) $scope.users = User.query() $scope.createUser = -> user = User.save($scope.newUser) $scope.users.push(user) 

    (This will add a new user record to the database by creating the createUser method)

  • Create a view to enter user input, write this code in home / index.html.erb.

     <div ng-controller="userCtrl"> <form ng-submit="createUser()"> <label>Enter Name</label> <input type="text" placeholder="Enter user name" ng-model="newUser.name" /> <button ng-click="createUser()" class="button tiny">Submit</button> </form> <!-- List all usres --> <h4 ng-show="users.$resolved && users.length > 1">All users</h4> <span ng-repeat="c in users"> {{c.name}}<br> </span> </div> 

Launch the application and view the clock.

+16
source

In this page, he gives the "Hello World" example for Angularjs + Rails scaffolding. (Please ignore additional details for additional functions.) When you enter any name, then receive the Hello message displayed for it, you have the Angularjs + rails functional application.

A few points:

  • A simple way to include an angular.js file:

    • Put the gem 'angularjs-rails' in your gemfile;

    • Then put //= require angular in your app/assets/javascripts/application.js file.

    • Most likely you will need //= require angular-resource so that you can use the ngResource services to access the RESTful API provided by the rails applicaiton from the back of the server. See this page for an example.

  • Include javascript codes specific to the Rails controller in your layout like this:

    • Add the following codes to the app/views/layouts/applications.html.erb file after the line <%= yield %> :

       <%= page_specific_js = "#{params[:controller]}_#{params[:action]}" %> <%= javascript_include_tag page_specific_js if Rails.application.assets.find_asset page_specific_js %> <%= stylesheet_link_tag page_specific_js if Rails.application.assets.find_asset page_specific_js %> 
    • Remove //= require_tree . from the .js application.

  • Include the following codes in config / initializers / assets.rb to precompile js and css files:

     Rails.application.config.assets.precompile << Proc.new do |path| if path =~ /\.(css|js)\z/ full_path = Rails.application.assets.resolve(path).to_path app_assets_path = Rails.root.join('app', 'assets').to_path if full_path.starts_with? app_assets_path puts "including asset: " + full_path true else puts "excluding asset: " + full_path false end else false end end 
0
source

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