【问题标题】:Controller can't see def new/create for rails 3.2.8 engine控制器看不到 def new/create for rails 3.2.8 引擎
【发布时间】:2013-01-10 17:37:40
【问题描述】:

我们正在 spec/dummy 下测试 customerx 引擎。引擎customerx的索引页可以正常显示。这是链接:

<li><%= link_to 'Customers', customerx.customer_status_categories_path %></li>

但是新客户链接出现路由错误uninitialized constant CustomerStatusCategoriesController,如下:

<li><%= link_to 'New Customer', customerx.new_customer_status_category_path %></li>

rake routes 确实显示了正确的新客户路线:

Routes for Customerx::Engine:
 customer_status_categories_index GET  /customer_status_categories/index(.:format)    customer_status_categories#index
   customer_status_categories_new GET  /customer_status_categories/new(.:format)      customer_status_categories#new
customer_status_categories_create GET  /customer_status_categories/create(.:format)   customer_status_categories#create
  customer_status_categories_edit GET  /customer_status_categories/edit(.:format)     customer_status_categories#edit
customer_status_categories_update GET  /customer_status_categories/update(.:format)   customer_status_categories#update
       customer_status_categories GET  /customer_status_categories(.:format)          customerx/customer_status_categories#index
                                  POST /customer_status_categories(.:format)          customerx/customer_status_categories#create
     new_customer_status_category GET  /customer_status_categories/new(.:format)      customerx/customer_status_categories#new
    edit_customer_status_category GET  /customer_status_categories/:id/edit(.:format) customerx/customer_status_categories#edit
         customer_status_category PUT  /customer_status_categories/:id(.:format)      customerx/customer_status_categories#update

在引擎 customerx 的 routes.rb 中,资源声明为:

 resources :customer_status_categories, :only => [:index, :new, :create, :edit, :update]

编辑/索引没有路由错误。新/创建所有通行证的 rspec 案例。问题好像是没有找到new的action(删除new的def和create后错误是一样的)。

导致错误的代码可能有什么问题?感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails-3 rails-engines


    【解决方案1】:

    通过在 routes.rb 中为 engine customerx 注释掉 get "customer_status_categories/new" 解决了这个问题:

    Customerx::Engine.routes.draw do
      get "customer_status_categories/index"
    
      #get "customer_status_categories/new"
    
      get "customer_status_categories/create"
    
      get "customer_status_categories/edit"
    
      get "customer_status_categories/update"
    
      resources :customer_status_categories
    
      root :to => 'customer_status_categories#index'
    
    end
    

    routes.rb 中的get ...new 在创建新控制器时由rails generate 自动插入。我们不知道为什么这条线会导致uninitialized constant 错误(但不会在其他如索引和编辑上)。有人可以阐明问题的原因吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      相关资源
      最近更新 更多