title: Update/destroy Rails model via AJAX call
body: <p>I'm trying to update my Rails model by using AJAX calls from front-end, but can't implement it correctly.</p>
  
  <p>My routes are:</p>
  
  <pre><code>  authenticated :customer do
      post 'update_card/:id', to: 'customers/cards#update'
      post 'delete_card/:id', to: 'customers/cards#destroy'
    end
  </code></pre>
  
  <p>Here's my controller that handles the requests:</p>
  
  <pre><code>class Customers::CardsController &lt; ApplicationController
    before_filter :authenticate_customer!
  
    def update
      if customer_signed_in?
        card_to_update = JSON.parse(params)
  
        card = current_customer.discount_cards.find(params[:id])
  
       # do the update... (update_attributes ?)
  
        render json: card
      end
      render status: :not_found, json: 'error'
    end
  
    def destroy
      # nothing yet
    end
  end
  </code></pre>
  
  <p>and this is how I do the AJAX call:</p>
  
  <pre><code>    $.ajax({
          type: 'POST',
          url: `/update_card/${this.props.cardId}.json`,
          data: JSON.stringify({
              name: newName,
              description: newDesc,
              shopName: newShopName,
          }),
          success: (msg) =&gt; {
              console.log('Data Saved: ' + msg);
          },
      });
  </code></pre>
  
  <p>I can't figure out what I'm missing and keep getting this error:</p>
  
  <blockquote>
    <p>TypeError - no implicit conversion of ActionController::Parameters
    into String</p>
  </blockquote>
  
  <hr>
  
  <p>Could you please help me with implementing the update and destroy methods correctly?</p>
  
  <hr>
  
  <h1>Update</h1>
  
  <p>I've changed my <code>update</code> method and now it looks like this:</p>
  
  <pre><code>  def update
      if customer_signed_in?
  
        card = current_customer.discount_cards.find(params[:id])
  
        card.name ||= params[:name]
        card.description ||= params[:description]
        card.shop.name = params[:shopName] if params[:shopName] &amp;&amp; card.shop
  
        card.save!
        current_customer.save!
  
        render json: { status: 'updated', object: card.to_json }
      else
        render json: { status: 'failed', errors: ['error'] }
      end
    end
  </code></pre>
  
  <p>and also now there's no <code>JSON.stringify</code> call in ajax</p>
  
  <p>However, the data doesn't get saved. Any ideas why?</p>