2016-11-09 4 views
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 

と私のルート

+1

私はあなたがIRB'が、私はあなたがレール – SteveTurczyn

+1

どのようにC '、 'したいと思います'たくないと思いますああ、うーん、恥ずかしい。初期! – Benjamints

答えて

0

irbは、Rails環境を自動的にロードしないため、モデル(またはヘルパー、データベースなど)にアクセスできません。しかし、「レールコンソールは」レールコンソールを起動するにはなど、あなたのRailsのクラス、データベース接続、すべての

をロードしIRBセッションです:

の省略形です
rails c 

rails console 

開発環境用のレールコンソールが起動します。

rails c RAILS_ENV=test 

や本番環境へ:あなたはそれがあなたのテスト環境に接続することができます

rails c RAILS_ENV=production 
関連する問題