Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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 Rails路由和has_one关系导致路由错误_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails Rails路由和has_one关系导致路由错误

Ruby on rails Rails路由和has_one关系导致路由错误,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,我有一个用户和橱柜模型 class User < ActiveRecord::Base has_one :cabinet after_create :create_cabinet // Omitted code def create_cabinet Cabinet.create(user_id: id) end end 每当我去我的用户文件柜作为文件柜返回时,我总是得到我的路线。1。。。然后,当我试图访问任何橱柜配料时,我得到一个错误,即橱柜配料.5(

我有一个用户和橱柜模型

class User < ActiveRecord::Base

  has_one :cabinet

  after_create :create_cabinet

  // Omitted code

  def create_cabinet
    Cabinet.create(user_id: id)
  end
end
每当我去我的用户文件柜作为文件柜返回时,我总是得到我的路线。1。。。然后,当我试图访问任何橱柜配料时,我得到一个错误,即橱柜配料.5(橱柜配料的id)找不到

不知道我为什么会得到这个。。我的
rake路由
返回:

    cabinet     POST   /cabinet(.:format)                 cabinets#create
    new_cabinet GET    /cabinet/new(.:format)             cabinets#new
   edit_cabinet GET    /cabinet/edit(.:format)            cabinets#edit
                GET    /cabinet(.:format)                 cabinets#show
                PUT    /cabinet(.:format)                 cabinets#update
                DELETE /cabinet(.:format)                 cabinets#destroy
橱柜展示视图

%table.table.table-striped
  %thead
    %tr
      %th My Ingredients
      %th ID
  %tbody
    - @cabinet.cabinet_ingredients.each do |ci|
      %tr
        %td= ci.ingredient.name
        %td= link_to "delete from cabinet", cabinet_path(ci), method: :delete, confirm: "Are you sure?"
机柜控制器:

  def show
    @cabinet = current_user.cabinet
  end

  def edit
    @cabinet = current_user.cabinet
    @ingredients = Ingredient.all
  end

  def update
    @ingredients = Ingredient.all
    @cabinet = current_user.cabinet
    if @cabinet.update_attributes(params[:cabinet])
      redirect_to @cabinet
    else
      render 'edit'
    end
  end

  def destroy
    ingredient = CabinetIngredient.find(params[:id])
    ingredient.destroy
    redirect_to cabinet_path
  end

如果有人有类似的问题,下面是我提出的解决方案:

我没有在机柜控制器中进行销毁,而是创建了一个机柜控制器并添加了销毁操作

class CabinetIngredientsController < ApplicationController
  def destroy
    ingredient = current_user.cabinet.cabinet_ingredients.find(params[:id])
    ingredient.destroy
    redirect_to cabinet_path
  end
end
最后是路线:

Mixology::Application.routes.draw do

  resource  :cabinet
  resources :cabinet_ingredients

end

您没有包含任何有关CAB_配料的路线或控制器信息,但问题很可能存在。如果您在路由中声明资源而不是资源,则只有一个文件柜,因此路由不希望您为文件柜使用id。能否显示要为路由调用的链接?添加了文件柜代码。我没有橱柜配料的路线,我在橱柜里做所有的工作
class CabinetIngredientsController < ApplicationController
  def destroy
    ingredient = current_user.cabinet.cabinet_ingredients.find(params[:id])
    ingredient.destroy
    redirect_to cabinet_path
  end
end
%table.table.table-striped
  %thead
    %tr
      %th My Ingredients
      %th ID
  %tbody
    - @cabinet.cabinet_ingredients.each do |ci|
      %tr
        %td= ci.ingredient.name
        %td= link_to "delete from cabinet", cabinet_ingredient_path(ci), method: :delete, confirm: "Are you sure?"
Mixology::Application.routes.draw do

  resource  :cabinet
  resources :cabinet_ingredients

end