为现有模型生成脚手架

时间:2013-02-05 15:58:38

标签: ruby-on-rails scaffolding

新手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

不确定如何调试此类问题......我认为路径在某种程度上搞砸了,但这就像我在这一点上可以理解的那样。帮助赞赏。

1 个答案:

答案 0 :(得分:0)

在我们公司,我们不使用脚手架,特别是当我们需要生成管理命名空间时 您可以自己编写管理命名空间。

配置/ routes.rb中

  namespace :admin do
      root :to => "base#index"
      resources :pages 
    # resources :states do
      # member do
        # get :make_default
      # end
    # end
  end

的应用程序/控制器/管理/ base_controller.rb

class Admin::BaseController < ApplicationController
 before_filter :authenticate_user!, :admin_user?


  layout "admin/admin"
  def index
    @page = Page.all
  end

private
  def admin_user?
   redirect_to root_path, :alert => 'This page is allowed for admin' unless current_user.admin
  end
end

的应用程序/视图/管理/碱/ index.html.haml

= link_to "New Post", new_admin_post_path
%ul
  - @post.each do |post|
    %li= post.title