NoMethodError未定义的方法

时间:2014-03-09 15:40:23

标签: ruby-on-rails ruby undefined

我正在做一个类似于Pinterest的网站,但是当我尝试用户根据他们的ID保存引脚时我得到了这个错误..我正在使用Rails 4 ...¿我如何解决这个错误?< / p>

NoMethodError in PinsController#create

undefined method `user_id=' for nil:NilClass

控制器/ pins_controllers.rb

# POST /pins.json
def create
  @pin.user_id = current_user.id
  @pin = Pin.new(pin_params)
  respond_to do |format|
end


模型/ pin.rb

class Pin < ActiveRecord::Base
  belongs_to :user  
  validates :photo, presence:true
  validates :description, presence:true
end


配置/ routes.rb中

Pinterest::Application.routes.draw do
  resources :pins
  devise_for :users

  root 'pages#index'
end


应用程序/控制器/ pins_controller.rb

   class PinsController < 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

分贝/迁移/ schema.rb

        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

我如何修复此错误?

NoMethodError in PinsController#create undefined method user_id ='为nil:NilClass`

1 个答案:

答案 0 :(得分:3)

您的行动应更改为:

# POST /pins.json
def create
  @pin = Pin.new(pin_params)
  @pin.user_id = current_user.id
end

因为您没有创建Pin的实例并希望为其指定值。