1

Estoy teniendo problemas con el metodo Destroy de mi applicación RoR. Teniendo una lista de restaurantes, y queriendo modificar uno de ellos pulsando en Edit, todas mis entradas son modificadas cuando solo quisiera modificar la seleccionada.

He aquí mi código:

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 %>
1
  • Podrias probar encerrando cada restaurant en una formulario ya que el método edit es un post de un form con los nuevos datos como atributos. el 17 ene. 2017 a las 14:42

1 respuesta 1

4

Lo que tu estas llamando es Restaurant.update(restaurant_params) esto genera un query update hacia todos tus registros de esa tabla.

Tienes que cambiar tu método update de la siguiente manera:

def update
  if @restaurant.update(restaurant_params)
    redirect_to restaurants_path
  else
    render :edit
  end
end
3
  • Si, lo vi. Muchas gracias.
    – mrngview
    el 20 ene. 2017 a las 10:20
  • 1
    Mi respuesta es la correcta?
    – roxdurazo
    el 20 ene. 2017 a las 22:54
  • @mrngview cuando tengas una respuesta correcta por favor no te olvides de marcarla para que sirva a toda la comunidad y como agradecimiento a quién te contesta. Saludos,
    – Lastimoso
    el 8 jul. 2017 a las 14:24

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.