title: curl POST create association request via terminal in rails app
body: <p>I am trying to create CRUD api for my app, which has two models: User and Group, which have many-to-many association through :memberships table.</p>
  
  <p>For authentication I use Devise (with :token_authenticatable option enabled)</p>
  
  <p>I want to test API using curl.</p>
  
  <p>Sign Up works perfectly</p>
  
  <pre><code>curl -v -H 'Content-Type: application/json' -H 'application/vnd.greenapp.v1' -X POST http://localhost:3000/api/registrations -d "{\"user\":{\"email\":\"user1@example.com\",\"name\":\"username\",\"password\":\"secret\",\"password_confirmation\":\"secret\"}}"
  </code></pre>
  
  <p>with response</p>
  
  <pre><code>{"success":true,"info":"Welcome! You have signed up successfully.","data":{"user":{"created_at":"2013-08-04T11:14:35Z","email":"user@example.com","id":2,"name":"username","updated_at":"2013-08-04T11:14:35Z"},"auth_token":"Eqp8jYsr5ExeNWbzrqef"}}* Closing connection #0
  </code></pre>
  
  <p>but I am struggling to find solution to create groups via API. I tried </p>
  
  <pre><code>curl -v -H 'Content-Type: application/json' -H 'application/vnd.greenapp.v1' -X POST http://localhost:3000/api/groups -d "{\"group\":{\"name\":\"groupname\",\"desc\":\"group description\"}}"
  </code></pre>
  
  <p>which returns (in server logs)</p>
  
  <pre><code>RuntimeError (Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id): app/controllers/api/v1/groups_controller.rb:49:in `create'
  </code></pre>
  
  <p>So here is my groups controller:</p>
  
  <pre><code>class Api::V1::GroupsController &lt; ApplicationController
    skip_before_filter :verify_authenticity_token,
                       :if =&gt; Proc.new { |c| c.request.format == 'application/json' }
  
    respond_to :json
  
    def new
      @group = Group.new
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @group }
      end
    end
  
    def create
      @group = Group.new(params[:group])
      @group.memberships.build(user_id: current_user.id)
      respond_to do |format|
        if @group.save
  
          format.html { redirect_to @group, notice: 'Group was successfully created.' }
          format.json { render json: @group, status: :created, location: @group }
        else
          format.html { render action: "new" }
          format.json { render json: @group.errors, status: :unprocessable_entity }
        end
      end
    end
  
  end
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>Update</strong></p>
  
  <p>SessionsCotroller</p>
  
  <pre><code>class Api::V1::SessionsController &lt; Devise::SessionsController
    skip_before_filter :verify_authenticity_token,
                   :if =&gt; Proc.new { |c| c.request.format == 'application/json' }
  
    respond_to :json
  
    def create
      warden.authenticate!(:scope =&gt; resource_name, :store =&gt; false, :recall =&gt; "#  {controller_path}#failure")
      render :status =&gt; 200,
         :json =&gt; { :success =&gt; true,
                    :info =&gt; t("devise.sessions.signed_in"),
                    :data =&gt; { :auth_token =&gt; current_user.authentication_token } }
    end
  
    def destroy
      warden.authenticate!(:scope =&gt; resource_name, :store =&gt; false, :recall =&gt; "#{controller_path}#failure")
      current_user.reset_authentication_token!
      render :status =&gt; 200,
         :json =&gt; { :success =&gt; true,
                    :info =&gt; t("devise.sessions.signed_out"),
                    :data =&gt; {} }
    end
  
    def failure
      render :status =&gt; 401,
         :json =&gt; { :success =&gt; false,
                    :info =&gt; "Login Failed",
                    :data =&gt; {} }
    end
  end
  </code></pre>