I am having problems with the Destroy method of my RoR application. Having a list of restaurants, and wanting to modify one of them by clicking Edit, all my entries are modified when I only want to modify the selected one.
Here's my code:
Controller:
class RestaurantsController < ApplicationController
before_action :set_restaurant, only: [:show, :edit, :update, :destroy]
def index
@restaurants = Restaurant.all
end
def show
end
def new
@restaurant = Restaurant.new
end
def create
Restaurant.create(restaurant_params)
redirect_to restaurants_path
end
def edit
end
def update
Restaurant.update(restaurant_params)
# raise
redirect_to restaurants_path
end
def destroy
@restaurant.destroy
redirect_to restaurants_path
end
end
private
def set_restaurant
@restaurant = Restaurant.find(params[:id])
end
def restaurant_params
params.require(:restaurant).permit(:name, :address, :stars)
end
Routes:
Prefix Verb URI Pattern Controller#Action
restaurants GET /restaurants(.:format) restaurants#index
POST /restaurants(.:format) restaurants#create
new_restaurant GET /restaurants/new(.:format) restaurants#new
edit_restaurant GET /restaurants/:id/edit(.:format) restaurants#edit
restaurant GET /restaurants/:id(.:format) restaurants#show
PATCH /restaurants/:id(.:format) restaurants#update
PUT /restaurants/:id(.:format) restaurants#update
DELETE /restaurants/:id(.:format) restaurants#destroy
Index.html.erb:
<h3>Liste des restaurants</h3>
<h3><%= link_to "Create a new restaurant", new_restaurant_path %></h3>
<% @restaurants.each do |restaurant| %>
<h2>--------------------------------------------------------------</h2>
<h3>Name: <%= restaurant.name %></h3>
<h3>Stars: <%= restaurant.stars %></h3>
<h3>Address: <%= restaurant.address %></h3>
<h3><%= link_to "See", restaurant_path(restaurant) %></h3>
<h3><%= link_to "Edit", edit_restaurant_path(restaurant) %></h3>
<h3><%= link_to "Delete", restaurant_path(restaurant), method: :delete, data: { confirm: "Are you sure?" } %></h3>
<% end %>