2017-08-06 54 views
-1

我试图在我的应用程序内创建一个多层控制器。Ruby:NoMethodError在BusinessAndUnitsController#index

具有以下结构:

--Business_and_units_controller (主控制器)

--↳Business_units_controller (副控制器)

----↳Business_managers_controller (副控制器到Business_units_controller)

----↳Business_divisions_controller (子控制器Business_units_controller)

----↳business_units_strats_attributes_controller (子控制器Business_units_controller)

这是很复杂的,也许过分?

不管怎么说,我已经创建了一个名为控制器:

business_and_units_controller.rb 

与模型:

business_and_unit.rb 

我已经添加了控制器我的routes.rb(测试它并看看它是否有效)

get '/testunit', to: 'business_and_units#index' 
    resources :business_and_units 

如果代码工作在看到我得到了以下错误:

NoMethodError in BusinessAndUnitsController#index 
undefined method `businessandunits' for #<User:0x007f55666f2a58> 

Extracted source (around line #6): 
1 
2    
3 
4 def index 
6 @business_and_units = current_user.businessandunits 
7  
8 end 

据我所知,这个问题的原因是,我business_and_units没有定义。但我不明白为什么没有定义。

你们能看到我的问题吗?


总结:

问题:根据我的红宝石是business_and_units_controller.rb没有索引定义。

目标本帖子中的内容:理解为什么没有定义。


PS:我已经搜索,并在搜索到我的问题的解决大量类似的职位,但一直没能找到解决的办法。

控制器文件

class BusinessAndUnitsController < ApplicationController 
    before_action :logged_in_user, only: [:create, :destroy] 
    before_action :correct_user, only: :destroy 

    def index 
    @business_and_units = current_user.business_and_units 

    end 

    def show 
     @business_and_units = Business_and_unit.find_by(id: params[:id]) 
     if [email protected]_and_unit 
     raise ActionController::RoutingError.new('Not Found') 
     end 
     @user = @business_and_unit.user 
    end 

    def new 
     @user = User.new 
     @business_and_unit = Business_and_unit.new 
    end 

    def edit 
     @business_and_unit = Business_and_unit.find(params[:id]) 
    end 

    def create 
     @business_and_unit = current_user.business_and_units.build(business_and_unit_params) 
     if @business_and_unit.save 
     flash[:success] = "business_and_unit created!" 
     redirect_to @business_and_unit 
     else 
     @feed_items = current_user.feed.paginate(page: params[:page]) 
     render 'new' 
     end 
    end 

    def update 
     @business_and_unit = Business_and_unit.find(params[:id]) 
     if @business_and_unit.update(business_and_unit_params) 
     redirect_to @business_and_unit 
     else 
     render 'edit' 
     end 
    end 

    def destroy 
     @business_and_unit.destroy 
     flash[:success] = "business_and_unit deleted" 
     redirect_to business_and_units_path 
    end 

    private 

    def business_and_unit_params 
     params.require(:business_and_unit).permit( 
     :corporate_strategy, :future_sale_value, 
     :industry, :market_focus, :business_unit, 

     business_units_attributes: [ 
     :id, :business_units, 
     :_destroy], 

     business_managers_attributes: [ 
     :id, :business_managers, 
     :_destroy], 

     business_divisions_attributes: [ 
     :id, :business_divisions, 
     :business_divisions_managers, 
     :_destroy], 

     business_units_strats_attributes: [ 
     :id, :business_units_strats, 
     :_destroy]) 
    end 

    def correct_user 
     @business_and_unit = current_user.business_and_units.find_by(id: params[:id]) 
     redirect_to business_and_units_path if @business_and_unit.nil? 
    end 

end 
+0

错误信息(其中约'current_user.businessandunits'投诉)和您提供的(看起来像在疑问,这行'current_user.business_and_units')执行代码不匹配。你的'User'模型是怎么样的?是否有q方法定义了“businessandunits”或“business_and_units”? – spickermann

+0

@spickermann嘿,谢谢你的回复。看来Andere回答了这个问题!但你在哪里进入同一个方向。谢谢! – Salman

回答

2

的问题无关,与你的控制器。

错误是您的User模型中没有方法#businessandunits

根据您提供的信息,我认为您错过了User类中的关系定义has_many :business_and_units

然后你就可以调用current_user.business_and_units

+1

谢谢安德烈!这似乎是我的一个快速监督。用户类确实错过了很多部分! – Salman