[英]<main>': uninitialized constant ActiveRecord (NameError)
I have the following model in my Rails 3.2.13 build. 我在Rails 3.2.13版本中有以下模型。 I am trying to use it to insert data into my database. 我试图用它来将数据插入我的数据库。
class Financials < ActiveRecord::Base
#attr_accessible :description, :stock
attr_accessible :symbol, :cur_price
sym = Financials.new(:symbol => test, :cur_price => 10)
sym.save
end
but when I try to run the code I get the following error: 但是当我尝试运行代码时,我收到以下错误:
financials.rb:1:in `': uninitialized constant ActiveRecord (NameError) financials.rb:1:in'':未初始化的常量ActiveRecord(NameError)
I checked through SO and found others that had similar errors and they suggested that I add entries in the environment.rb ruby on rails pluralization help? 我检查了SO,发现其他人有类似的错误,他们建议我在环境中添加条目.rb ruby on rails multipleization help?
I added the following to the environment.rb file: 我将以下内容添加到environment.rb文件中:
Inflector.inflections do |inflect|
inflect.irregular 'financialss', 'financials'
end
but it did resolve my issue. 但它确实解决了我的问题。 Thanks in advance 提前致谢
You don't create new objects inside the definition of the model. 您不在模型定义中创建新对象。 You should be doing this in the create
action of the controller. 您应该在控制器的create
动作中执行此操作。
Given your model: 鉴于您的型号:
class Financial < ActiveRecord::Base
attr_accessible :symbol, :cur_price
# validations, methods, scope, etc.
end
You create the new Financial
object in your controller and redirect to the appropriate path: 您在控制器中创建新的Financial
对象并重定向到适当的路径:
class FinancialsController < ApplicationController
def create
@financial = Financial.new(params[:financial])
if @financial.save
redirect_to @financial
else
render :new
end
end
def new
@financial = Financial.new
end
def show
@financial = Financial.find(params[:id])
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.