Angular - Rails值不会出现在生产中

时间:2015-11-19 15:32:23

标签: ruby-on-rails angularjs ruby-on-rails-4 heroku

将应用程序部署到heroku时,我遇到了一个奇怪的问题。

因此,有两种方法可以创建订单。首先,无论何时付款,都会自动创建订单。这完全是通过Rails完成的。其次,Orders索引视图包含一个显示所有订单的表,以及一个用于创建新订单的表单,该表使用Angular。实际上,它更像是使用Angular进行练习的实验。

所以,整个过程在开发中运行良好。所有订单(通过两种方法创建)都显示正常,以及相应的用户ID,产品ID和总数(订单将花费的金额)。

但是,当我部署应用程序时,我遇到了一个奇怪的错误。一切正常,但通过付款创建的订单总数不会显示。这意味着,对于所有订单,用户ID和产品ID都会显示,而对于使用Angular创建的订单,订单总数会显示出来。仅对于没有Angular创建的订单,订单总数不会出现。

非常感谢一些帮助!!

资产/ JavaScript的/ angular_app.js

var app = angular.module('shop', ['ngResource']);

app.factory('models', ['$resource', function($resource){
    var orders_model = $resource("/orders/:id.json", {id: "@id"});
    var products_model = $resource("/products/:id.json", {id: "@id"});
    var users_model = $resource("/users/:id.json", {id: "@id"});
    var x = {
        orders: orders_model,
        products: products_model,
        users: users_model
    };
    return x;
}]);

app.controller('OrdersCtrl', ['$scope', 'models', function($scope, models){
    $scope.orders = models.orders.query();
    $scope.products = models.products.query();
    $scope.users = models.users.query();
    $scope.addOrder = function(){
        order = models.orders.save($scope.newOrder, function(){
            recent_order = models.orders.get({id: order.id});
            $scope.orders.push(recent_order);
            $scope.newOrder = '';
        });
    }
    $scope.deleteOrder = function(order){
        models.orders.delete(order);
        $scope.orders.splice($scope.orders.indexOf(order), 1);
    }
}]);

orders_controller.rb

class OrdersController < ApplicationController
    protect_from_forgery
    skip_before_action :verify_authenticity_token, if: :json_request?
    respond_to :json, :html

    def index
        @orders = Order.all.to_json(:include => [{:product => {:only => :name}},
                                                 {:user => {:only => :email}}])
        respond_with @orders
    end

    def show
        @order = Order.find(params[:id]).to_json(:include => [{:product => {:only => :name}},
                                                              {:user => {:only => :email}}])
        respond_with @order
    end

    def new
    end

    def create
        @order = Order.create(order_params)
        @order.product = Product.find(params[:product_id])
        @order.user = User.find(params[:user_id])  

        OrderMailer.order_confirmation(@order.product, @order.user.email, @order.user.first_name)

        respond_with @order
    end

    def destroy
        respond_with Order.destroy(params[:id])
    end

    protected

    def json_request?
        request.format.json?
    end

    private

    def order_params
        params.require(:order).permit(:product_id, :user_id, :total)
    end
end

订单/ index.html.erb

<div ng-controller="OrdersCtrl">
    <table class="table table-hover">
        <thead>
            <td>Order ID</td>
            <td>Total</td>
            <td>Product</td>
            <td></td>
        </thead>
        <tr>
            <form ng-submit="addOrder()">
                <td>
                    <span class="form-control" disabled>
                        <%= Order.last.id + 1 %>
                    </span>
                </td>
                <td>
                    <input type="number" step="0.01" class="form-control" ng-model="newOrder.total">
                </td>
                <td>
                    <select ng-model="newOrder.product_id" class="form-control">
                        <option value="" disabled selected>Select a product</option>
                        <option ng-repeat="product in products" value="{{product.id}}">{{product.name}}</option>
                    </select>
                </td>
                <td>
                    <select ng-model="newOrder.user_id" class="form-control">
                        <option value="" disabled selected>Select a user</option>
                        <option ng-repeat="user in users" value="{{user.id}}">{{user.id}}</option>
                    </select>
                </td>
                <td>
                    <input type="submit" value="+" class="btn btn-success">
                </td>
            </form>
        </tr>
        <tr ng-repeat="order in orders | orderBy: '-id':reverse">
            <td>
                {{order.id}}
            </td>
            <td>
                <strong>{{order.total | currency}}</strong>
            </td>
            <td>
                {{order.product.name}}
            </td>
            <td>
                {{order.user.email}}
            </td>
            <td>
                <button ng-click="deleteOrder(order)" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
            </td>
        </tr>
    </table>
</div>

配置/环境/ production.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Enable Rack::Cache to put a simple HTTP cache in front of your application
  # Add `rack-cache` to your Gemfile before enabling this.
  # For large-scale production use, consider using a caching reverse proxy like
  # NGINX, varnish or squid.
  #config.action_dispatch.rack_cache = true

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true

  # Asset digests allow you to set far-future HTTP expiration dates on all assets,
  # yet still be able to expire them through the digest params.
  config.assets.digest = true

  # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  # Prepend all log lines with the following tags.
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups.
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production.
  config.cache_store = :dalli_store,
                        (ENV["MEMCACHIER_SERVERS"] || "").split(","),
                        {:username => ENV["MEMCACHIER_USERNAME"],
                         :passowrd => ENV["MEMCACHIER_PASSWORD"],
                         :failover => true,
                         :socket_timeout => 1.5,
                         :socket_failure_delay => 0.2
                        }

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = 'http://assets.example.com'

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false
  config.assets.js_compressor = Uglifier.new(mangle: false)
end

0 个答案:

没有答案