Rails AJAX PUT / PATCH错误

时间:2014-03-23 03:34:09

标签: ruby-on-rails ajax coffeescript

我正在尝试从一个coffeescript向一个rails控制器发送一个PATCH(或PUT,我已尝试过两次)。我得到NoM​​ethodError:

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()

控制器看起来像这样(我还没修改它,所以它应该是库存):

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。有人能告诉我我做错了吗?

2 个答案:

答案 0 :(得分:0)

试试:

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

您正在将字符串传递给数据,而不是传递Javascript对象。

答案 1 :(得分:0)

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

NoMethodError (undefined method `permit' for "{\"accountBalance\":49}":String):
app/controllers/credit_cards_controller.rb:72:in `credit_card_params'

问题在于您在ajax中设置数据的方式。它需要像这样结构:

params{ "credit_card" => { "accountBalance" => "49", "pin" => "0000" }  }

我建议这样做:

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