-1

I have the following personal class to work on an arrangement for the shopping cart, but when I try to call a method of this class in my controller it indicates an undefined method add_cesta.

This is my class

class Carro
  attr_reader :cesta

  def initialize
    @cesta = []
  end

  def add_cesta(articulo)
    @cesta << articulo
  end

end

and this is my controller

class TiendaController < ApplicationController


  def index
    @titulo = "Bienvenido a la Tienda"
    @articulos = Articulo.all.order("nombre").page(params[:page]).per_page(4)    
  end

  def quienes_somos
    @titulo = "Bienvenido a la Tienda"      
  end

  def contacto
    @titulo = "Bienvenido a la Tienda"
  end

  def anadir_producto
      @articulo = Articulo.find(params[:id])
      @carro = sesion_carrito
      @carro.add_cesta(@articulo)
      flash[:info] ="Producto añadido #{@articulo.nombre}"      
      redirect_to inicio_url
  end

  def ver_carro    
      @carro = session[:carro]    
  end

  def vaciar_carrito
      session[:carro] = nil
      flash[:info] = "Carrito vacio"
      redirect_to inicio_url
  end

  private

  def sesion_carrito
      session[:carro] ||= Carro.new
  end
end
Chava Geldzahler
  • 3,605
  • 1
  • 18
  • 32

2 Answers2

0

I believe you didn't actually set the instance variable to a cart.

Perhaps the session[:carro] contains something else than a cart? Could you post the full error message please as this would clarify your problem quite a bit.

For now try setting:

@carro = Carro.new

And afterwards refactor the session part a bit.

-- Edit --

Seems like the session variable is a string instead of an actual object.

Try storing the cart_id in your session and retrieve it later on.

def ver_carro
  @carro ||= (Carro.find(session[:carro_id]) || Carro.new)
end

with something like that.

LewisFidlers
  • 59
  • 1
  • 3
  • Hello thanks for replying, based on what you indicate me actually session [: carro] is saving something different the first time I run it does not give error but when I try to add another it throws this error undefined method `add_cesta' for "#":String i will try to find out what is saving; if you know a method to know the value of a session variable in rails I really appreciate if you indicate me the how.. Sorry my english I understand but I'm not very good writing it. – Angel Bastidas Sep 06 '17 at 21:44
-1

You don't have access to the method within the controller as it's defined in a separate class. You would need to explicitly provide access by requiring the file within the controller class. Put the path to the actual file within the quotes below. See similar question here: Including a Ruby class from a separate file

require 'file_path/carro'
  class TiendaController < ApplicationController
Stephen W
  • 244
  • 2
  • 7
  • thanks for replying but in this case does not apply because it is in model, theoretically rails include it – Angel Bastidas Sep 06 '17 at 21:45
  • You still should need to require it unless it's in the load path and the file is named properly. And if it's a model then it looks like you possibly haven't initialized the model correctly. class Carro< ActiveRecord::Base – Stephen W Sep 07 '17 at 02:26