1
IRBに新しい 'ピン'を作成しようとしているときにこのエラーが発生します。たとえば:irbは私のモデルにアクセスできません(NameError:初期化されていない定数)
irb(main):001:0> @pin = Pin.first
NameError: uninitialized constant Pin
OR
irb(main):001:0> @pin = Pin.new
NameError: uninitialized constant Pin
私はそれが前に働いていたとして、何かを変更する必要があります。残念ながら、私はエラーここ
を見つけることができないで、私のピンコントローラ:ここ
class PinsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :set_pin, only: [:show, :edit, :update, :destroy]
def index
@pins = Pin.all
end
def show
@pin = Pin.find params[:id]
end
def new
@pin = Pin.new
end
def edit
end
def create
@pin = Pin.new(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find(params[:id])
redirect_to pins_path, notice: "Not allowed!" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description)
end
end
は私の団体、pin.rb
class Pin < ApplicationRecord
belongs_to :user
end
そしてUser.rbのための私の団体です:
は、class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :pins
end
と私のルート
私はあなたがIRB'が、私はあなたがレール – SteveTurczyn
どのようにC '、 'したいと思います'たくないと思いますああ、うーん、恥ずかしい。初期! – Benjamints