Ruby irb无权访问我的模型(NameError:未初始化常量)
我在尝试在IRB中创建新的“Pin”时收到此错误。例如:Ruby irb无权访问我的模型(NameError:未初始化常量),ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,我在尝试在IRB中创建新的“Pin”时收到此错误。例如: irb(main):001:0> @pin = Pin.first NameError: uninitialized constant Pin 或 我必须改变一些东西,因为它以前是有效的。不幸的是,我找不到错误 这是我的PIN控制器: class PinsController < ApplicationController before_action :authenticate_user!, except: [:in
irb(main):001:0> @pin = Pin.first
NameError: uninitialized constant Pin
或
我必须改变一些东西,因为它以前是有效的。不幸的是,我找不到错误
这是我的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
irb
不会自动加载Rails环境,这就是为什么它无法访问您的模型(或助手、数据库或其他任何内容)。但是,“rails控制台”是一个irb会话,它确实加载所有rails类、数据库连接等
要启动rails控制台,请执行以下操作:
rails c
这是以下的简写:
rails console
这将启动开发环境的rails控制台。您可以使其连接到您的测试环境:
rails c RAILS_ENV=test
rails c RAILS_ENV=production
或到您的生产环境:
rails c RAILS_ENV=test
rails c RAILS_ENV=production
我不认为你想要
IRB
,我认为你想要rails c
多么尴尬,哦。早期!
rails c RAILS_ENV=test
rails c RAILS_ENV=production