2017-08-11 43 views
0

問題AJAX更新不正確傳遞參數的SQL調用在不同的環境

所以我使用AJAX來更新namephone爲屬性的組織。該更新在本地正常工作,但不在生產中。

數據

日誌我得到本地:

Started PUT "/organizations/14" for ::1 at 2017-08-11 08:03:18 -0700 
Processing by OrganizationsController#update as JSON 
    Parameters: {"organization"=>{"name"=>"Org Name", "phone"=>"3434234322"}, "id"=>"14"} 
    User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 195], ["LIMIT", 1]] 
    Organization Load (0.5ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."user_id" = 195 LIMIT $1 [["LIMIT", 1]] 
    (0.2ms) BEGIN 
    SQL (9.2ms) UPDATE "organizations" SET "phone" = $1, "updated_at" = $2 WHERE "organizations"."id" = $3 [["phone", "3434234322"], ["updated_at", 2017-08-11 15:03:18 UTC], ["id", 14]] 
    (0.5ms) COMMIT 
Completed 200 OK in 21ms (Views: 0.8ms | ActiveRecord: 11.0ms) 

日誌我得到生產:

Started PUT "/organizations/1" for [censored] at 2017-08-11 15:12:58 +0000 
Processing by OrganizationsController#update as JSON 
    Parameters: {"organization"=>{"name"=>"Org Name", "phone"=>"12121212"}, "id"=>"1"} 
    User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 48], ["LIMIT", 1]] 
    Organization Load (0.9ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."user_id" = 48 LIMIT $1 [["LIMIT", 1]] 
    (0.6ms) BEGIN 
    SQL (0.9ms) UPDATE "organizations" SET "updated_at" = $1 WHERE "organizations"."id" = $2 [["updated_at", 2017-08-11 15:12:58 UTC], ["id", 1]] 
    (1.7ms) COMMIT 
Completed 200 OK in 12ms (Views: 0.6ms | ActiveRecord: 4.9ms) 

我的思想

所以這裏的差異似乎是SQL調用(第7行)。在當地,電話被正確傳遞,就像這樣:

SQL (9.2ms) UPDATE "organizations" SET "phone" = $1, "updated_at" = $2 WHERE "organizations"."id" = $3 [["phone", "3434234322"], ["updated_at", 2017-08-11 15:03:18 UTC], ["id", 14]]

但在生產,這不是......

SQL (0.9ms) UPDATE "organizations" SET "updated_at" = $1 WHERE "organizations"."id" = $2 [["updated_at", 2017-08-11 15:12:58 UTC], ["id", 1]]

我想不通爲什麼會在本地工作,但不是生產。同樣值得注意的是,如果我爲name進行AJAX更新,它可以在兩種環境中都能正常工作。我最近添加了phone到數據庫,並且這個特定的組織在創建時沒有phone作爲屬性,所以我認爲這可能與它有關。但是我可以在數據庫中看到該屬性,並可以爲其分配值並保存,而不會有任何問題。除非我錯了,這正是我將它傳遞給控制器​​時所要做的。

相關代碼

我的AJAX調用:

$.ajax({ 
    url: `/organizations/${organization.id}`, 
    method: 'PUT', 
    data: { "organization": { 
     "name": nameValue, 
     "phone": phoneValue 
    }}, 
    dataType: 'json', 
    success: (data) => { 
     // do stuff 
    } 
}); 

我的控制器(導軌):

class OrganizationsController < ApplicationController 
    before_action :set_organization, only: [:edit, :update] 
    skip_before_action :verify_authenticity_token 

    def edit 
    end 

    def update 
     respond_to do |format| 
      if @organization.update(organization_params) 
       format.html { redirect_to '/dashboard', notice: 'Organization was successfully updated.' } 
       format.json { render json: @organization } 
      else 
       format.html { render '/dashboard' } 
       format.json { render json: @organization.errors, status: :unprocessable_entity } 
      end 
     end 
    end 

    private 
    def set_organization 
     @organization = Organization.find_by(user: current_user) 
    end 

    def organization_params 
     params.require(:organization).permit(:name, :phone) 
    end 

end 

我只加上面的AJAX調用,因爲我肯定這就是所需要的。但對於完整起見,這裏更多的是做出反應成分的它用在:

import React from 'react'; 

export default class OrganizationInput extends React.Component{ 

    constructor(props) { 
     super(props); 
     this.state = { 
      nameValue: this.props.organization.name, 
      phoneValue: this.props.organization.phone, 
      xhr: false, 
      faClass: null, 
     } 
    } 

    handleInput =(event, input)=> { 
     this.setState({[input]: event.target.value 

      // creates font awesome spinner 
      this.handleFaChange() 

      const { organization } = this.props 
      const { nameValue, phoneValue } = this.state 

      if(this.state.xhr !== false) {clearTimeout(this.state.xhr)} 

      this.state.xhr = setTimeout(() => { 
       $.ajax({ 
        url: `/organizations/${organization.id}`, 
        method: 'PUT', 
        data: { "organization": { 
         "name": nameValue, 
         "phone": phoneValue 
        }}, 
        dataType: 'json', 
        success: (data) => { 
         this.setState({xhr: false}) 

         // creates font awesome check mark 
         this.handleFaChange() 
        } 
       }); 
      }, 300) 
     }) 
    } 

    render() { 
     const { organization } = this.props 
     return (
      <div> 
       <input value={this.state.nameValue} className="organization-input" placeholder={`Barbershop Name`} onChange={(e)=> {this.handleInput(e, "nameValue")}}/> 
       <input value={this.state.phoneValue} className="organization-input" placeholder={`Phone Number`} onChange={(e)=> {this.handleInput(e, "phoneValue")}}/> 
      </div> 
     ); 
    } 
} 

回答

0

難道phone屬性添加到organizations表後的應用程序來重新啓動?

此外,您可能希望將if @organization.update(organization_params)更改爲if @organization.update!(organization_params),因此如果驗證失敗,將引發異常。

+0

是的,它重新啓動。沒有做任何事情。感謝您的更新電話提示。我確實找出了一個解決方案,但(種類)。根本不會更新在添加電話屬性及其相應遷移之前創建的任何組織。我只寫了一個快速腳本來克隆以前在數據庫中的所有組織,並將所有關聯重新分配給這些組織。這是一個解決方案,爲我解決了這個問題,但我仍然很好奇爲什麼我必須這樣做。我可以從控制檯更新組織,但不能直接從應用程序中更新。 – Doug

+0

就好像正在運行的應用程序沒有看到那個新的字段......但是即使這樣也不應該是你重新啓動它的問題。這確實是一個困惑。 –

相關問題