Rails:NameError:未初始化的常量贷款

时间:2015-07-16 19:39:31

标签: ruby-on-rails ruby model nameerror

我是RoR的新手,我的一个模特遇到了一些困难。我已经阅读了几个关于此错误的问题,但我认为我的命名约定正确,而其他问题导致了这个问题。

当我在控制台(我已多次重启)并输入" Loans" (复数)我得到NameError:未初始化的常量贷款,当我输入"贷款" (单数)我得到:

NameError: undefined local variable or method `class_name' for #<Class:0x00000005701520>

模型/ loan.rb

class Loan < ActiveRecord::Base
  belongs_to :lender, class_name => 'User'
  belongs_to :borrower, class_name => 'User'
end

控制器/ loans_controller.rb

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

分贝/ schema.rb

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

3 个答案:

答案 0 :(得分:1)

belongs_to :lender, class_name => 'User'

应该是:

belongs_to :lender, :class_name => 'User'

或更简洁:

belongs_to :lender, class_name: 'User'

答案 1 :(得分:0)

在我的模型中,我写了class_name而不是:class_name。对不起,如果我浪费了人们的时间,希望这可以帮助别人!

答案 2 :(得分:0)

如果是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

在您的情况下,您已经发现类名应该是地图符号,如:class =&gt; 'class_name'或类:'class_name'或更好的方式class :: ClassName