- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在 IRB 中尝试创建新的“Pin”时收到此错误。例如:
irb(main):001:0> @pin = Pin.first
NameError: uninitialized constant Pin
或
irb(main):001:0> @pin = Pin.new
NameError: uninitialized constant Pin
我必须改变一些东西,因为它以前是有效的。不幸的是,我找不到错误
这是我的引脚 Controller :
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
和我的路线
Rails.application.routes.draw do
resources :pins
devise_for :users
root "pages#home"
get "about" => "pages#about"
end
最佳答案
irb
不会自动加载您的 Rails 环境,这就是为什么它无法访问您的模型(或助手、数据库或其他任何东西)的原因。但是,“rails 控制台”是一个 irb session ,它会加载所有 Rails 类、数据库连接等。
启动 Rails 控制台:
rails c
这是以下内容的简写:
rails console
这将为您的开发环境启动 Rails 控制台。你可以让它连接到你的测试环境:
rails c RAILS_ENV=test
或您的生产环境:
rails c RAILS_ENV=production
关于ruby - irb 无权访问我的模型(NameError : uninitialized constant),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40511503/
虽然我试图完全理解 NoMethodError 和 NameError 之间的区别,但我发现 NameError 在祖先中不在 0 位置。是 DidYouMean 的魔法吗? [1] pry(main
请谁来帮我解决这个问题。**当在服务器上运行时,特定的结果会出现,但服务器运行得很好。当我运行代码时,它发送这个错误
下面的 Python 由于某种原因失败了。 class NetVend: def blankCallback(data): pass def sendCommand(c
作为this great answer建议,从 Python 3.7 开始,如果 from __future__ import annotations 使用指令。 但是,如果我想为注释类型创建别名,这
我在导入自制模块时遇到了一些麻烦,我只是看不出我做错了什么。 我有一个名为 basics 的包,其中包含我所有的基类 我有一个名为 components 的第二个包,components 中的每个模块
我的 python 代码不断出现名称错误,未在 ticketSold 上定义全局变量。我不确定如何解决这个问题,因为我确实将其定义为全局变量。感谢您的帮助。 aLimit=300 bLimit=500
我正在安装 Redmine,但由于抽成问题,我快疯了。特别是,为了对不同的 Ruby 插件执行捆绑安装,我被要求安装 xapian-full-alaveteli,v 1.2.9.5。问题是我收到以下错
我的 python 代码不断出现名称错误,未在 ticketSold 上定义全局变量。我不确定如何解决这个问题,因为我确实将其定义为全局变量。感谢您的帮助。 aLimit=300 bLimit=500
我正在安装 Redmine,但由于抽成问题,我快疯了。特别是,为了对不同的 Ruby 插件执行捆绑安装,我被要求安装 xapian-full-alaveteli,v 1.2.9.5。问题是我收到以下错
所以我是第一年,有一点编码经验,但不多,我被赋予了制作基于文本的冒险游戏的任务。我的讲师给了我们一个模板并帮助我们开始,但我很困惑为什么这段代码会导致 shell 显示 NameError: name
我有一些编程经验,但我对 python 很陌生,我正在尝试弄清楚如何使用和导入 .py 文件中的类而不是 main 。我目前正在使用 netbeans,运行 CPython 3.2.1。 根据我现在的
代码非常简单,我刚刚开始用 python 编程 代码 man = input ("what's your name mister") print("his name is "+man) 运行程序后收到
我在文件中创建一个类。声明一些类变量 A = 5 和另一个类变量 B = A+1。从另一个文件导入该类时,出现 NameError: name 'A' is not Defined。有没有办法解决这个
我一直在为 Minecraft 的 Raspberry Pi 版本制作 mod,每次在程序中输入其中一个命令时,我都会遇到非常令人沮丧的错误。这是我的代码: import minecraft.mine
为什么会这样: def fn(proc, *args, **kwargs): cache = proc.cache = {} def cached_execution(cache, *
我是 Python 的新手,正在学习类,正在尝试编写一个“个人信息”程序: 这是我的代码: class PersonalInfo(): def names(self, name):
我今天用这个简单的代码遇到了一个非常奇怪的问题: var1 = 1 var2 = 2 if var1 > var2 > var3: print('Does not run') print('D
我正在 try catch 无法加载模块时发生的任何异常。当前的结果是“except” block 没有被执行。 import sys def loadModule(module): try:
这是我的代码: import os if os.path.exists(r'C:\Genisis_AI'): print("Main File path exists! Continuing
我正在学习列表理解,通过切换变量我得到了 2 个不同的结果,尽管它们看起来应该工作相同。 数组 a 等于 [[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 0
我是一名优秀的程序员,十分优秀!