Ruby on rails ActiveRecord::在IngredientsController中未找到Record#销毁
我是Rails的新手。我希望任何人都能帮助我,我相信,当我弄明白为什么它不能像我预期的那样工作时,这是合乎逻辑的 我有一个销毁方法,实际上是WPRK。它破坏了我想要的,但我得到一个错误,无法找到id 提前谢谢 错误: ActiveRecord::在IngredientsController中未找到Record#销毁 找不到id为38的配料 我的配料控制器:Ruby on rails ActiveRecord::在IngredientsController中未找到Record#销毁,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我是Rails的新手。我希望任何人都能帮助我,我相信,当我弄明白为什么它不能像我预期的那样工作时,这是合乎逻辑的 我有一个销毁方法,实际上是WPRK。它破坏了我想要的,但我得到一个错误,无法找到id 提前谢谢 错误: ActiveRecord::在IngredientsController中未找到Record#销毁 找不到id为38的配料 我的配料控制器: def destroy @ingredient = Ingredient.find(params[:id]) @ingredient
def destroy
@ingredient = Ingredient.find(params[:id])
@ingredient.destroy
flash[:notice] = @ingredient.ingredient + ' is succesvol verwijderd'
redirect_to '/recipes/'
end
我的路线:
Website::Application.routes.draw do
root 'recipes#index'
resources :recipes, only: [:destroy, :edit, :update, :create, :new, :index, :show]
do
resources :ingredients
end
get 'ingredients/:id/destroy' => 'ingredients#destroy'
get '/find' => 'recipes#find'
get 'recipes/find' => 'recipes#find'
get 'recipes/:id/destroy' => 'recipes#destroy'
post 'recipes/:id/edit' => 'recipes#update'
get 'recipes/edit' => 'recipes#edit'
get 'recipes/update' => 'recipes#index'
post 'recipes/new' => 'recipes#create'
get 'ingredients/show' => 'ingredients#show'
get 'ingredients/create' => 'recipes#edit'
get 'ingredients/edit' => 'ingredients#edit'
html.erb中的代码
<table>
<th>ing_id</th>
<th>Ingredient</th>
<th>Hoeveelheid</th>
<th>Eenheid</th>
<th>recipe_id</th>
<th>delete</th>
<% @ingredient.each do |ingredients| %>
<tr>
<td><%= ingredients.id %></td>
<td><%= ingredients.ingredient %></td>
<td><%= ingredients.amount %></td>
<td><%= ingredients.unit %></td>
<td><%= ingredients.recipe_id %></td>
<td><a href='/ingredients/<%= ingredients.id %>/destroy'>Delete</a></td>
</tr>
<% end %>
ing_id
成分
霍韦尔海德
伊恩海德
配方id
删除
由于您在调用flash之前已经销毁了@component
的实例,因此您的flash[:注意]无法找到要渲染的@component。改为这样做:
flash[:notice] = @ingredient.ingredient + ' is succesvol verwijderd'
@ingredient.destroy
您是否检查了
@component=component.find(params[:id])
是否可用。检查来自视图的参数。尝试在rails控制台中使用相同的代码来验证结果和数据库内容。我怀疑destroy
操作不是问题所在。正如您所说,它正在正常工作并删除资源。它看起来像是:
您正在尝试在删除的成分模型被销毁后访问它。这将导致ActiveRecord::RecordNotFound
错误,因为销毁操作的第一件事就是获取资源
或
您正在尝试再次删除相同的成分模型。如果您在资源的显示页面上,并在销毁资源后刷新它,您将获得
ActiveRecord::RecordNotFound
,因为该资源已不存在。这是我的重定向中的一个问题(或者老实说,可能有时我尝试了很多)。但最终起作用的是让我的控制器看起来像这样:
def destroy
@ingredient = Ingredient.find(params[:id])
@ingredient.destroy
flash[:notice] = @ingredient.ingredient + ' is succesvol verwijderd'
redirect_to edit_recipe_path(@recipe)
end
你在flash中使用@Component后销毁它在销毁它之前备份它并在flash中使用该备份实例,或者只交换
flash[:注意]
和`.destroy`的行。destroy`你知道你只能将和除了传递给资源?比如,resource:components,only:[:show,:update,:destroy]
@pduersteler,我不知道这个。但是在这种情况下它能帮我什么忙呢?嗨,谢谢你的快速回复。我试过你的解决办法,但不幸的是,那不是问题所在。我甚至删掉了那个闪光通知,也没用。我对食谱也做了同样的事情。在它之后销毁并通知,就像在我的控制器中一样。它在那里工作。所以我盯着屏幕看,看不懂。你能把你的堆栈跟踪包括进去吗?