RoR 的新手,對發生的事情有點困惑。
我正在按照這里的教程:https : //www.javatpoint.com/ruby-on-rails-crud 創建一個 crud 應用程式。在第 17 步,我必須在終端中運行“rake db:migrate”。每次我收到錯誤訊息“NameError: uninitialized constant Products mount Products::ProductsAPI => '/api/products'”
我suggestion here:https://stackoverflow.com/questions/35978598/rake-aborted-nameerror-uninitialized-constant-users 在一個較舊的職位上作業過。除了模型 product.rb (\crud\app\models\product.rb) 之外,所有其他類和模塊都標記為“產品”。我準確地復制了代碼并使用“gem update --system”更新了gem,以防它是導致問題的Grape gem。我將分享以下檔案:
我的產品控制器
class ProductsController < ApplicationController
# GET method to get all products from database
def index
@products = Product.all
end
# GET method to get a product by id
def show
@product = Product.find(params[:id])
end
# GET method for the new product form
def new
@product = Product.new
end
# POST method for processing form data
def create
@product = Product.new(product_params)
if @product.save
flash[:notice] = 'Product added!'
redirect_to root_path
else
flash[:error] = 'Failed to edit product!'
render :new
end
end
# GET method for editing a product based on id
def edit
@product = Product.find(params[:id])
end
# PUT method for updating in database a product based on id
def update
@product = Product.find(params[:id])
if @product.update_attributes(product_params)
flash[:notice] = 'Product updated!'
redirect_to root_path
else
flash[:error] = 'Failed to edit product!'
render :edit
end
end
# DELETE method for deleting a product from database based on id
def destroy
@product = Product.find(params[:id])
if @product.delete
flash[:notice] = 'Product deleted!'
redirect_to root_path
else
flash[:error] = 'Failed to delete this product!'
render :destroy
end
end
# we used strong parameters for the validation of params
def product_params
params.require(:product).permit(:name, :price, :old_price, :short_description, :full_description)
end
end
我的產品型號:
class Product < ApplicationRecord
validates :name, presence: true
validates :price, presence: true, numericality: {:greater_than => 0}
validates :short_description, presence: true
end
我的遷移資料庫表:
class CreateProducts < ActiveRecord::Migration[7.0]
def change
create_table :products do |t|
t.string :name
t.decimal :price
t.text :short_description
t.text :full_description
t.timestamps
end
end
end
我的路線檔案:
Rails.application.routes.draw do
get 'products/index'
get 'products/show'
get 'products/new'
get 'products/create'
get 'products/edit'
get 'products/update'
get 'products/destroy'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
resources :products
root 'products#index'
# Defines the root path route ("/")
mount Products::ProductsAPI => '/api/products'
# root "articles#index"
end
我按照信中的步驟進行操作,但無法運行 rake db:migrate。
我更改了遷移檔案名和類名,以便它們匹配 - 沒有運行
我在路線檔案中更新了路線的位置,沒有運行并得到同樣的錯誤。
我查看并更新了代碼以完全匹配 javtpoint 教程中的步驟并且它沒有運行。
我嘗試在管理員中運行終端以防它有幫助
我將檔案夾的完整位置放在路由檔案中,而不是“ProductsAPI => '/api/products'”我有 C:\Users\Desktop\CRUD\CRUD\crud\app\api\products
此外,更新了 gems 并在之后運行 bundle install 。就第 17 步“rake db:migrate”而言,一切都按設計進行。
非常感謝您的幫助!
uj5u.com熱心網友回復:
它無法從第 15 步中找到此檔案:
應用程式/api/products/products_api.rb
您可以從 Rails 控制臺快速驗證這一點:
$ rails console
[1] development(main)> Products::ProductsAPI
NameError: uninitialized constant Products
這個檔案存在嗎?
另外,我不認為現在需要步驟 4 中的config.autoload_paths配置。/app下的任何自定義目錄都會在 Rails 7 中自動加載:
https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#config-autoload-paths
注意: 本教程看起來是基于 2015 年左右的 Rails 4 或 5,而您使用的是 Rails 7。嘗試在 Rails 7 中實作本教程時可能會遇到其他問題。我建議您查找更現代的教程。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/531220.html
標籤:轨道上的红宝石视窗耙
