我在我的应用程序中添加了一个名为Tokened.rb
的模块,并将include Tokened
添加到我的模型中。但是,现在当我尝试加载该模型时,我得到一个“TestingsController #index中的NameError”错误...我没有在我的TestingsController中包含Tokened,但不确定为什么我应该或我应该把它放在哪里。
我的代码:
testing.rb
class Testing < ActiveRecord::Base
include Tokened
end
My Tokened.rb模块:
module Tokened
extend ActiveSupport::Concern
included do
after_initialize do
self.token = generate_token if self.token.blank?
end
end
private
def generate_token
loop do
key = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
break key unless self.class.find_by(token: key)
end
end
end
最后,我的测试控制器:
class TestingsController < ApplicationController
before_action :set_testing, only: [:show, :edit, :update, :destroy]
# GET /testings
# GET /testings.json
def index
@testings = Testing.all
end
# GET /testings/1
# GET /testings/1.json
def show
end
# GET /testings/new
def new
@testing = Testing.new
end
# GET /testings/1/edit
def edit
end
# POST /testings
# POST /testings.json
def create
@testing = Testing.new(testing_params)
respond_to do |format|
if @testing.save
format.html { redirect_to @testing, notice: 'Testing was successfully created.' }
format.json { render :show, status: :created, location: @testing }
else
format.html { render :new }
format.json { render json: @testing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /testings/1
# PATCH/PUT /testings/1.json
def update
respond_to do |format|
if @testing.update(testing_params)
format.html { redirect_to @testing, notice: 'Testing was successfully updated.' }
format.json { render :show, status: :ok, location: @testing }
else
format.html { render :edit }
format.json { render json: @testing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /testings/1
# DELETE /testings/1.json
def destroy
@testing.destroy
respond_to do |format|
format.html { redirect_to testings_url, notice: 'Testing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_testing
@testing = Testing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def testing_params
params.require(:testing).permit(:name, :address, :signature)
end
end
是什么给出的?我不确定这里发生了什么以及为什么它需要包含在控制器中。
答案 0 :(得分:0)
首先,它应该是小写的:tokened.rb
。但你的文件在哪里?它应该在modules/concerns/tokened.rb
。