Я новичок в Rails. Я надеюсь, что кто-то может мне помочь, и я уверен, что это логично, как только я понял, почему он не работает, как я ожидаю.ActiveRecord :: RecordNotFound in IngredientsController # destroy
У меня есть метод уничтожения, который на самом деле wprks. Он уничтожает то, что я хочу, но я получаю сообщение об ошибке, что идентификатор не найден.
Заранее спасибо.
Ошибка:
ActiveRecord :: RecordNotFound в IngredientsController # уничтожают Не удалось найти ингредиент с ID = 38
Мой контроллер ингредиент:
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 %>
U используется @ingredient вспышка после уничтожения его –
сделать резервную копию этого, прежде чем уничтожить его и использовать эту резервную копию экземпляра вспышки –
Или просто поменять местами строки , 'flash [: notice]' и '.destroy' – rlecaro2