title: NoMethodError undefined method
body: <p>I'm doing a website similar to Pinterest, but when I try that the users save the pins according to their id I get this error.. I´m using Rails 4... ¿How I fix this error?</p>
  
  <pre><code>NoMethodError in PinsController#create
  
  undefined method `user_id=' for nil:NilClass
  </code></pre>
  
  <p>controllers/pins_controllers.rb</p>
  
  <pre><code># POST /pins.json
  def create
    @pin.user_id = current_user.id
    @pin = Pin.new(pin_params)
    respond_to do |format|
  end
  </code></pre>
  
  <p><br>
  model/pin.rb</p>
  
  <pre><code>class Pin &lt; ActiveRecord::Base
    belongs_to :user  
    validates :photo, presence:true
    validates :description, presence:true
  end
  </code></pre>
  
  <p><br>
  config/routes.rb</p>
  
  <pre><code>Pinterest::Application.routes.draw do
    resources :pins
    devise_for :users
  
    root 'pages#index'
  end
  </code></pre>
  
  <p><br></p>
  
  <p>app/controllers/pins_controller.rb</p>
  
  <pre><code>   class PinsController &lt; ApplicationController
       before_action :set_pin, only: [:show, :edit, :update, :destroy]
       before_filter :authenticate_user!, except: [:index, :show]
       # GET /pins
       # GET /pins.json
       def index
         @pins = Pin.all
       end
  
       # GET /pins/1
       # GET /pins/1.json
       def show
       end
  
       # GET /pins/new
       def new
         @pin = Pin.new
       end
  
       # GET /pins/1/edit
       def edit
       end
  
       # POST /pins
       # POST /pins.json
       def create
         @pin = Pin.new(pin_params)
         @pin.user_id = current_user.id
         respond_to do |format|
           if @pin.save
             format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
             format.json { render action: 'show', status: :created, location: @pin }
           else
             format.html { render action: 'new' }
             format.json { render json: @pin.errors, status: :unprocessable_entity }
           end
         end
       end
  
      # PATCH/PUT /pins/1
      # PATCH/PUT /pins/1.json
      def update
        respond_to do |format|
          if @pin.update(pin_params)
            format.html { redirect_to @pin, notice: 'Pin was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: 'edit' }
            format.json { render json: @pin.errors, status: :unprocessable_entity }
          end
        end
      end
  
      # DELETE /pins/1
      # DELETE /pins/1.json
      def destroy
        @pin.destroy
        respond_to do |format|
          format.html { redirect_to pins_url }
          format.json { head :no_content }
        end
      end
  
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_pin
          @pin = Pin.find(params[:id])
        end
  
        # Never trust parameters from the scary internet, only allow the white list                  through.
        def pin_params
          params.require(:pin).permit(:name, :photo, :description)
        end
    end
  </code></pre>
  
  <p>db/migrate/schema.rb</p>
  
  <pre><code>        ActiveRecord::Schema.define(version: 20140308225255) do
  
            create_table "pins", force: true do |t|
              t.string   "photo"
              t.string   "description"
              t.datetime "created_at"
              t.datetime "updated_at"
              t.string   "name"
            end
  
            create_table "users", force: true do |t|
              t.string   "email",                  default: "", null: false
              t.string   "encrypted_password",     default: "", null: false
              t.string   "reset_password_token"
              t.datetime "reset_password_sent_at"
              t.datetime "remember_created_at"
              t.integer  "sign_in_count",          default: 0,  null: false
              t.datetime "current_sign_in_at"
              t.datetime "last_sign_in_at"
              t.string   "current_sign_in_ip"
              t.string   "last_sign_in_ip"
              t.datetime "created_at"
              t.datetime "updated_at"
              t.string   "phone"
            end
  
            add_index "users", ["email"], name: "index_users_on_email", unique: true
            add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  
          end
  </code></pre>
  
  <p>How I fix this error? </p>
  
  <p><code>NoMethodError in PinsController#create undefined method</code>user_id=' for nil:NilClass`</p>