Ruby on rails Rails AJAX放置/修补程序错误

Ruby on rails Rails AJAX放置/修补程序错误,ruby-on-rails,ajax,coffeescript,Ruby On Rails,Ajax,Coffeescript,我尝试发送补丁或PUT,我尝试了从coffeescript到rails控制器的AJAX调用。我得到一个命名者: Started PUT "/credit_cards/2" for 127.0.0.1 at 2014-03-22 22:12:02 -0500 Processing by CreditCardsController#update as JSON Parameters: {"credit_card"=>"{\"accountBalance\":49}", "id"=>"

我尝试发送补丁或PUT,我尝试了从coffeescript到rails控制器的AJAX调用。我得到一个命名者:

Started PUT "/credit_cards/2" for 127.0.0.1 at 2014-03-22 22:12:02 -0500
Processing by CreditCardsController#update as JSON
  Parameters: {"credit_card"=>"{\"accountBalance\":49}", "id"=>"2"}
  CreditCard Load (0.2ms)  SELECT "credit_cards".* FROM "credit_cards" WHERE "credit_cards"."id" = ? LIMIT 1  [["id", "2"]]
Completed 500 Internal Server Error in 2ms

NoMethodError (undefined method `permit' for "{\"accountBalance\":49}":String):
  app/controllers/credit_cards_controller.rb:72:in `credit_card_params'
  app/controllers/credit_cards_controller.rb:44:in `block in update'
  app/controllers/credit_cards_controller.rb:43:in `update'
我的AJAX调用设置如下:

accountJson = "{\"accountBalance\":#{@account.accountBalance}"
$.ajax "/credit_cards/#{@selectedCard}",
    type:'PUT'
    dataType:'json'
    data: 
        'credit_card' : accountJson
    error:(jqXHR, textStatus, errorThrown)->
        alert "AJAX Error: #{textStatus}"
        currentAtm.fundsNotWithdrawn()
    success: (data, textStatus, jqXHR) ->
        currentAtm.fundsWithdrawn()
params{ "credit_card" => { "accountBalance" => "49", "pin" => "0000" }  }
控制器看起来像这样,我还没有修改它,所以它应该是库存的:

class CreditCardsController < ApplicationController
  before_action :set_credit_card, only: [:show, :edit, :update, :destroy]

  # GET /credit_cards
  # GET /credit_cards.json
  def index
    @credit_cards = CreditCard.all
  end

  # GET /credit_cards/1
  # GET /credit_cards/1.json
  def show
  end

  # GET /credit_cards/new
  def new
    @credit_card = CreditCard.new
  end

  # GET /credit_cards/1/edit
  def edit
  end

  # POST /credit_cards
  # POST /credit_cards.json
  def create
    @credit_card = CreditCard.new(credit_card_params)

    respond_to do |format|
      if @credit_card.save
        format.html { redirect_to @credit_card, notice: 'Credit card was successfully created.' }
        format.json { render action: 'show', status: :created, location: @credit_card }
      else
        format.html { render action: 'new' }
        format.json { render json: @credit_card.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /credit_cards/1
  # PATCH/PUT /credit_cards/1.json
  def update
    respond_to do |format|
      if @credit_card.update(credit_card_params)
        format.html { redirect_to @credit_card, notice: 'Credit card was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @credit_card.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /credit_cards/1
  # DELETE /credit_cards/1.json
  def destroy
    @credit_card.destroy
    respond_to do |format|
      format.html { redirect_to credit_cards_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_credit_card
      @credit_card = CreditCard.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def credit_card_params
      params.require(:credit_card).permit(:accountBalance, :pin)
    end
end
我使用的是Rails v4.0.4。有人能告诉我我做错了什么吗?

试试看:

accountJson = {"accountBalance": "#{@account.accountBalance}"}

您向数据传递的是字符串,而不是Javascript对象。

错误不在于ajax,而是控制器对数据的处理:

NoMethodError (undefined method `permit' for "{\"accountBalance\":49}":String):
app/controllers/credit_cards_controller.rb:72:in `credit_card_params'
问题在于在ajax中设置数据的方式。它需要这样的结构:

accountJson = "{\"accountBalance\":#{@account.accountBalance}"
$.ajax "/credit_cards/#{@selectedCard}",
    type:'PUT'
    dataType:'json'
    data: 
        'credit_card' : accountJson
    error:(jqXHR, textStatus, errorThrown)->
        alert "AJAX Error: #{textStatus}"
        currentAtm.fundsNotWithdrawn()
    success: (data, textStatus, jqXHR) ->
        currentAtm.fundsWithdrawn()
params{ "credit_card" => { "accountBalance" => "49", "pin" => "0000" }  }
我建议:

accountJson = {credit_card: {accountBalance: @account.accountBalance}}
$.ajax "/credit_cards/#{@selectedCard}",
    type:'PUT'
    dataType:'json'
    data: accountJson

您的代码中有一个小的输入错误,在变量名后留下了一个大括号,但是在修复了这个错误之后,我仍然会得到一个错误。我发送的参数如下所示:参数:{{\credit\u card\:{\accountBalance\:\48\}=>nil,id=>2}。这可能是因为JSON解析器对其进行了字符串化-如果您只发送accountJSON而不使用stringify函数会发生什么?啊哈!当我不严格控制对象时,它就像一个符咒。因此,数据行应该是:data:accountJson