[英]Rails: NameError: uninitialized constant Loans
I am new to RoR and am having some difficulty with one of my models. 我是RoR的新手,并且在使用其中一个模型时遇到了一些困难。 I have read several questions regarding this error but I think I have my naming conventions correct and that something else is causing the issue. 我已经阅读了有关此错误的几个问题,但我认为我的命名约定正确,并且其他原因导致了此问题。
When I am in the console (which I have restarted several times) and type "Loans" (plural) I get the NameError: uninitialized constant Loans, and when I type "Loan" (singular) I get: 当我在控制台中(已重新启动几次)并输入“ Loans”(复数)时,我得到了NameError:未初始化的常量贷款,而当我键入“ Loan”(单数)时,我得到了:
NameError: undefined local variable or method `class_name' for #<Class:0x00000005701520>
class Loan < ActiveRecord::Base
belongs_to :lender, class_name => 'User'
belongs_to :borrower, class_name => 'User'
end
class LoansController < ApplicationController
before_action :set_loan, only: [:show, :edit, :update, :destroy]
# GET /loans
# GET /loans.json
def index
@loans = Loan.all
end
# GET /loans/1
# GET /loans/1.json
def show
end
# GET /loans/new
def new
@loan = Loan.new
end
# GET /loans/1/edit
def edit
end
# POST /loans
# POST /loans.json
def create
@loan = Loan.new(loan_params)
respond_to do |format|
if @loan.save
format.html { redirect_to @loan, notice: 'Loan was successfully created.' }
format.json { render :show, status: :created, location: @loan }
else
format.html { render :new }
format.json { render json: @loan.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /loans/1
# PATCH/PUT /loans/1.json
def update
respond_to do |format|
if @loan.update(loan_params)
format.html { redirect_to @loan, notice: 'Loan was successfully updated.' }
format.json { render :show, status: :ok, location: @loan }
else
format.html { render :edit }
format.json { render json: @loan.errors, status: :unprocessable_entity }
end
end
end
# DELETE /loans/1
# DELETE /loans/1.json
def destroy
@loan.destroy
respond_to do |format|
format.html { redirect_to loans_url, notice: 'Loan was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_loan
@loan = Loan.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def loan_params
params[:loan]
end
end
ActiveRecord::Schema.define(version: 20150716190548) do
create_table "loans", force: :cascade do |t|
t.integer "lender_id"
t.integer "borrower_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "amount"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
end
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
add_index "users", ["username"], name: "index_users_on_username", unique: true
end
belongs_to :lender, class_name => 'User'
should be: 应该:
belongs_to :lender, :class_name => 'User'
or more concisely: 或更简而言之:
belongs_to :lender, class_name: 'User'
In my model I wrote class_name
instead of :class_name
. 在我的模型中,我写了class_name
而不是:class_name
。 Sorry if I wasted people's time, hope this can help someone else! 抱歉,如果我浪费别人的时间,希望这可以对其他人有所帮助!
In case of has_one and belongs_to the model name comes in singular form and in case of has_many and has_many_through model name will comes in plural form for eg 在has_one和belongs_to的情况下,模型名称采用单数形式;在has_many和has_many_through情况下,模型名称采用复数形式,例如
Class User
has_many :posts
has_one :account
end
Class Post
belongs_to :user
has_many :comments
end
Class Comment
belongs_to :post
end
Class Account
belongs_to :user
end
In your case as you already found the class name should be map symbol like :class => 'class_name' or class: 'class_name' or in better way class: :ClassName 在您的情况下,您已经发现类名称应该是地图符号,例如:class =>'class_name'或class:'class_name'或更好的方式class::ClassName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.