У меня есть две таблицы, recey и recipe_ingredients, recipe_ingredients настроен как вложенный ресурс
resources :recipies do
resources :recipe_ingredients
end
Recipy_ingredients отображаются на моей странице recey_edit, и я также хочу удалить их оттуда.Мое представление для редактирования:
<% @recipe_ingredients.each do |ing| %>
<p> <%= ing.amount %> <%= ing.unit %> <%= ing.ingredient.try(:ing_name) %> </p>
<p><%= link_to ('<i class="material-icons"> clear </i>').html_safe, recipy_recipe_ingredient_path(ing.recipy, ing),
method: :delete, data: {confirm: 'Are you sure?'} %>
</p>
<% end %>
</div>
Моя правка для правки из контроллера receies_controller:
def edit
@recipy = Recipy.find(params[:id])
@recipe_ingredients = @recipy.recipe_ingredients
end
для уничтожения из контроллера recipe_ingredients:
def destroy
@recipe_ingredient = RecipeIngredient.find(params[:id])
recipy = @recipe_ingredient.recipy
@recipe_ingredient.destroy
redirect_to recipy
end
при попытке открытьстраница редактирования, я получаю сообщение об ошибке ниже msg:
Не найдено ни одного маршрута {: action => «show»,: controller => «recipe_ingredients»,: id => nil,: recey_id => «160»},пропущены обязательные ключи: [: id]
и эта строка выделена:
<p><%= link_to ('<i class="material-icons"> clear </i>').html_safe, recipy_recipe_ingredient_path(ing.recipy, ing),
Не запутан ли маршрут?Я думал, что это будет правильный Snytax для вложенных ресурсов?Любая помощь будет оценена.Благодарю.Дайте мне знать, что любая дополнительная информация необходима!
РЕДАКТИРОВАТЬ
После некоторого копания я решил, что если я удалю частичное перед link_to в редактируемом виде, то кодработает отлично.Моя часть перед кодом:
Добавить новый ингредиент
<%= form_with model:[@recipy, @recipy.recipe_ingredients.build] do |form| %>
<div class="field">
<%= form.label :amount %>
<%= form.text_field :amount %>
</div>
<div class="field">
<%= form.label :unit %>
<%= form.text_field :unit %>
</div>
<div class="field">
<%= form.select(:ingredient_id, options_from_collection_for_select(Ingredient.all, :id, :ing_name))%>
</div>
<div class="actions">
<%= form.submit "add ingredient", remote: true %>
</div>
<% end %>
и создать def для recipe_ingredients:
def create
@recipy = Recipy.find(params[:recipy_id])
@recipe_ingredient = @recipy.recipe_ingredients.new(recipe_ingredient_params)
respond_to do |f|
if @recipe_ingredient.save
@recipe_ingredients = @recipy.recipe_ingredients.last
# f.html
# f.json
f.js { render :template => "/recipies/updatingview.js.erb", layout: false}
end
end
end
Почему это испортит функцию удаления?Если я удаляю эту часть, удаление действительно работает.А совет?