Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby:BusinessAndUnitsController索引中的NoMethodError_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Ruby:BusinessAndUnitsController索引中的NoMethodError

Ruby on rails Ruby:BusinessAndUnitsController索引中的NoMethodError,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试在我的应用程序中创建一个多层控制器 结构如下: --业务单元和单元控制器(主控制器) --↳ 业务单元控制器(子控制器) ----↳ 业务部经理部总监(副总监至业务部总监) ----↳ 业务部门主管(副主管至业务部门主管) ----↳ 业务单元战略属性控制器(子控制器到业务单元控制器) 这很复杂,可能太复杂了 总之,我创建了一个名为: business_and_units_controller.rb 使用模型: business_and_unit.rb 我已将控制器添加到我的rou

我正在尝试在我的应用程序中创建一个多层控制器

结构如下:

--业务单元和单元控制器(主控制器)

--↳ 业务单元控制器(子控制器)

----↳ 业务部经理部总监(副总监至业务部总监)

----↳ 业务部门主管(副主管至业务部门主管)

----↳ 业务单元战略属性控制器(子控制器到业务单元控制器)

这很复杂,可能太复杂了

总之,我创建了一个名为:

business_and_units_controller.rb
使用模型:

business_and_unit.rb
我已将控制器添加到我的routes.rb(以测试它是否工作)

在查看代码是否有效后,我发现以下错误:

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
业务和单元控制器索引中的命名错误 未定义的方法“businessandunits”# 提取的源(第6行附近): 1. 2. 3. 4 def索引 6@business_和_units=当前_用户。businessandunits 7. 8结束 我知道问题的原因是我的业务单位和单位没有定义。但我不明白为什么它没有定义

你们谁能看出我问题的原因吗


总结如下:

My问题:根据ruby,索引中未定义My business_和_units_controller.rb

这篇文章的目标:理解为什么没有定义它


附言:我已经搜索了很多类似的帖子来寻找我问题的解决方案,但一直没有找到解决方案

我的控制器文件

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 !@business_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
类BusinessAndUnitsController
问题与控制器无关

错误在于您的
用户
模型中没有方法
#businessandunits

根据您提供的信息,我认为您遗漏了一个关系定义
在您的
用户
类中有许多:业务和单位


然后,您将能够调用
当前用户。业务和单位

错误消息(投诉
当前用户。业务和单位
)和您提供的代码(看起来像这
当前用户。相关行的业务和单位
)不匹配。您的
用户
型号看起来怎么样?是否定义了q方法
businessandunits
businessandunits
。安德尔似乎回答了这个问题!但你要去的是同一个方向。非常感谢。谢谢你,安德烈!看来这是我的疏忽。用户类确实缺少了很多部分!
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 !@business_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