2016-07-28 77 views
1

我有一個應用程序使用AngularJS的訂單頁面,它通過JSON API連接到我的SQLite3/Postrgres數據庫在Rails中。我可以從Angular頁面創建和刪除訂單,但是我無法更新訂單發貨列(複選框)。當我點擊該複選框時,JS控制檯會指示出貨值從false更改爲true,但它會返回錯誤:POST ... /orders/1.json 404(Not Found)AngularJS POST更新到數據庫在Rails

我認爲這個問題可能與Rails orders_controller中的代碼有關。

這是我的代碼。

index.html.erb

<input type="checkbox" value="order.shipped" ng-click="changeShipped(order)"> 

app.js

// change shipped status via checkbox 
$scope.changeShipped = function(order) { 
    order.shipped = !order.shipped; 
    models.orders.save(order); // returns POST... 404 (Not Found) 
    console.log(order.shipped); // indicates true/false 
} 

orders_controller.rb

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

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

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

# def new 
# end 

def create 
    @order = Order.create(order_params) 
    respond_with @order 
end 

def update 
    @order = Order.find(params[:id]) 
    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, :shipped) 
    end 
end 

階模型

t.integer "user_id" 
t.integer "product_id" 
t.decimal "total" 
t.boolean "shipped", default: false, null: false 
t.index ["product_id"], name: "index_orders_on_product_id" 
t.index ["user_id"], name: "index_orders_on_user_id" 

的routes.rb

Rails.application.routes.draw do 
    devise_for :users, :controllers => { :registrations => "user_registrations" } 
    resources :users 
    resources :orders, only: [:index, :show, :create, :update, :destroy] 

    resources :products do 
    resources :comments 
    end 

    root 'static_pages#index' 

    get '/search', to: 'static_pages#search' 

    get '/search_results', to: 'products#search_results' 

    get '/about', to: 'static_pages#about' 

    get '/contact', to: 'static_pages#contact' 

    post 'static_pages/thank_you' 

    devise_scope :user do 
    get '/sign_up', to: 'devise/registrations#new' 
    get '/profile', to: 'devise/registrations#edit' 
    end 

    post '/payments/create', to: 'payments#create' 
    get 'payments/payment_thank_you', to: 'payments#payment_thank_you' 
end 
+0

你能不能也發表您的路線? – Finks

+0

當然,見上面 – Christian

回答

1

從我可以告訴,看來你發送一個POST請求update行動但默認情況下,Rails是期待PUTPATCH請求該操作。

然而,大多數的瀏覽器不支持HTTP實時通過AJAX PUTPATCH要求,所以關鍵是要增加,具體_method以張貼的JSON數據。

{_method:'put', order: order_data } Ruby on rails - PUT method on update ajax

+0

這可能是正確的。我怎麼會我的JS? '$ scope.changeShipped = function(order){' – Christian

+0

你需要檢查models.orders.save(order),因爲這段代碼似乎在發送ajax請求。您需要更新它,以便它發送PUT請求而不是POST。 – Finks