title: Can't get user_id in database after Ajax call
body: <p>In javascript I do an ajax call to the create function of deliveries_controller. This puts a new Delivery in the database with a product and quantity. I also try to put the current_user as user_id in the database, but for some reason it stays nil in the database.</p>
  
  <p>My ajax call:</p>
  
  <pre><code>$.ajax({
    type: "POST",
    url: "/deliveries",
    data: { delivery: {ingredient: "meel", quantity: "800", scenario_id: "3"} },
    success: function(){
      alert('Success!');
    },
    error: function(){
      alert('No success');
    }
  });
  </code></pre>
  
  <p>I just pass some dummy data to test it all out.</p>
  
  <p>and my deliveries_controller:</p>
  
  <pre><code>class DeliveriesController &lt; ApplicationController
  protect_from_forgery
  
    def index
      @storages = Storage.where(user_id: current_user)
    end
  
  
    def addQuantity
     @storage = Storage.where(user_id: current_user.id)
     @storage.update_all ("quantity = (quantity+200)")
     redirect_to deliveries_url
    end
  
    def create
  
      @delivery = Delivery.new(delivery_params)
      respond_to do |format|
      if @delivery.save
        format.html do
          render :nothing =&gt; true
        end
        format.json { render json: @delivery.to_json }
      else
        format.html { render :nothing =&gt; true} ## Specify the format in which you are rendering "new" page
        format.json { render json: @delivery.errors } ## You might want to specify a json format as well
      end
  
    end
  
  end
  
  private
  
    def delivery_params
        params.require(:delivery).permit(:user_id, :ingredient, :quantity, :scenario_id)
    end
  
  end
  </code></pre>
  
  <p>New entries are created in the database, but whichever way I try to pass the user_id as param it isn't saved in the database.</p>
  
  <p>I tried it like:
  <code>@delivery = Delivery.new(delivery_params, :user_id =&gt; current_user)</code>,</p>
  
  <pre><code>@user_id = current_user
  @delivery = Delivery.new(delivery_params, @user_id)
  </code></pre>
  
  <p>and</p>
  
  <p><code>params.require(:delivery).permit(:user_id, :ingredient, :quantity, :scenario_id).merge(user_id: current_user)</code></p>
  
  <p>log:
  <code>Started POST "/deliveries" for 127.0.0.1 at 2014-11-03 12:59:37 +0100
  Processing by DeliveriesController#create as */*
    Parameters: {"delivery"=&gt;{"ingredient"=&gt;"meel", "quantity"=&gt;"800", "scenario_id"=&gt;"3"}}
  Can't verify CSRF token authenticity
    User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."remember_token" = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' LIMIT 1
    CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."remember_token" = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' LIMIT 1
     (0.0ms)  begin transaction
    SQL (0.2ms)  INSERT INTO "deliveries" ("created_at", "ingredient", "quantity", "scenario_id", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", "2014-11-03 11:59:37.253274"], ["ingredient", "meel"], ["quantity", 800], ["scenario_id", 3], ["updated_at", "2014-11-03 11:59:37.253274"]]
     (12.5ms)  commit transaction
    Rendered text template (0.0ms)
  Completed 200 OK in 24ms (Views: 0.8ms | ActiveRecord: 13.1ms)
  </code></p>
  
  <p>but the user_id for Delivery stays nil. How would I pass the user_id from the current_user so it's saved in the database with the json I retrieve from the ajax call?
  Thanks in advance</p>
  
  <p>EDIT:
  I fixed it the following way:
  I send json data to javascript with content_tag:
  <code>= content_tag(:div,"", id: "storages", data:{url: Storage.where(user_id: current_user)})</code>
  this data is handled, and the user_id is suddenly accepted :)</p>
  
  <p>thanks for the help!</p>