RoR 的新手。我已经构建了没有命名空间的模型。其中之一被称为“品牌”。然后我继续使用 rails g "admin/brands" 将维护功能放在管理命名空间下,使用rails generate scaffolding_controller "admin/brand"
- 生成视图和控制器。当我进行测试时,单元测试失败:
NoMethodError: undefined method `admin_brands' for #<Admin::BrandsControllerTest:0x1034c0730>
test/functional/admin/brands_controller_test.rb:5:in `_callback_before_193'
在 routes.rb 我有:
# Administration routes
namespace :admin do
resources :brands
end
生成的控制器代码如下:
class Admin::BrandsController < ApplicationController
# GET /admin/brands
# GET /admin/brands.json
def index
@admin_brands = Brand.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @admin_brands }
end
end
# GET /admin/brands/1
# GET /admin/brands/1.json
def show
@admin_brand = Brand.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @admin_brand }
end
end
# GET /admin/brands/new
# GET /admin/brands/new.json
def new
@admin_brand = Brand.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @admin_brand }
end
end
# GET /admin/brands/1/edit
def edit
@admin_brand = Brand.find(params[:id])
end
# POST /admin/brands
# POST /admin/brands.json
def create
@admin_brand = Brand.new(params[:admin_brand])
respond_to do |format|
if @admin_brand.save
format.html { redirect_to @admin_brand, :notice => 'Brand was successfully created.' }
format.json { render :json => @admin_brand, :status => :created, :location => @admin_brand }
else
format.html { render :action => "new" }
format.json { render :json => @admin_brand.errors, :status => :unprocessable_entity }
end
end
end
# PUT /admin/brands/1
# PUT /admin/brands/1.json
def update
@admin_brand = Brand.find(params[:id])
respond_to do |format|
if @admin_brand.update_attributes(params[:admin_brand])
format.html { redirect_to @admin_brand, :notice => 'Brand was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @admin_brand.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /admin/brands/1
# DELETE /admin/brands/1.json
def destroy
@admin_brand = Brand.find(params[:id])
@admin_brand.destroy
respond_to do |format|
format.html { redirect_to admin_brands_url }
format.json { head :no_content }
end
end
end
不知道如何调试此类问题......我认为路径以某种方式搞砸了,但这就是我目前所能理解的。帮助表示赞赏。