"Stack level too" deep with @controller.hi

Hi again, sorry for many questions. In mi HomeController, I have the "hi" method, in this method I use an "if" for show a different message for a registered user or a guest. When I call in "home/index" my method "hi", show me this error message:

"Stack level too deep"

class HomeController < ApplicationController   def hi     if (current_user)       @username = current_user_session.user.username       @welr = '¡Bienvenid@ ' + @username.to_s + ' a nuestra web!'       render 'home/index'     else       @weli = "¡Bienvenid@ invitad@, no dude en registrarse!"     end   end end

# home/index <p>Holaaaa</p> <%= @controller.hi %>

Well, I have trying find the solution, but I haven't can. Can anyone help me?

Rewards, Iván

most likely

@username = current_user_session.user.username

which will probably work better as

@username = current_user.username

and

@welr = ‘¡Bienvenid@ ’ + @username.to_s + ’ a nuestra web!’

is better as

@welr = “¡Bienvenid@ #{@username} a nuestra web!”

You are rendering home/index which is calling hi which is rendering home/index which is calling hi which is rendering home/index which is calling hi which is rendering home/index which is calling hi which is rendering home/index which is calling hi which is rendering home/index which is calling hi which is…

The whole thing is wrong anyway. The “hi” method should be a helper and not a controller method, it shouldn’t render anything either. Using @controller should be avoided for the kind of thing you want to do (if used at all to start off with, unless you want to show some logging in your views in development mode)

All you want is

class HomeController < ApplicationController def index

end end

class HomeHelper < ApplicationHelper

def hi

end

end

home/index

Holaaaa

<%= hi %>

Best regards

Peter De Berdt

Lo que estas tratando de hacer aqui se hace en los helpers no en el control , las funciones que se desea usar para generar texto o algun valor en la vista se colocan en los helper, los controles se usan para procesar request y no tiene sentido hacer que desde el index se llame a hi pues generas un request para index y cuando este se genera llamas a hi con otro request que de hecho necesita una ruta y un render para funcionar como quieres, pero la solucion mas simple es poner esta funcion en un helper.

tambien puedes hacerlo de esta forma

def index

@welcome = hi

end

private

def hi

if user_signed_in?

return “¡Bienvenid@ #{current_user.username} a nuestra web!”

else

return “¡Bienvenid@ invitad@, no dude en registrarse!”

end

end

y en la vista

Holaaaa

<%= @welcome %>

nunca interpoles (sustituyas variables en cadenas) usando + (signo de mas), en ruby siempre es preferible user #{ }.

Esta forma que he puesto es por si insisten en usar la funcion desde el control, como dije antes, las funciones en los controles que se acceden desde la vista siempre son para procesar request, no los generes sin pensar la secuencia que se crea en el server. Si desea funciones en a vista usa los helpers, declaralas ahi y luego llamalas desde la vista , estaran accecible sin problema. En los controlles pueden poner funcione para procesar sierta logica y no request, y lo que se hace es que solo se hace para acceder de desde el control mismo y se protegen con un private.

superclass must be a Class (Module given)
Hi Martin, Peter and Radhames, I understand your explanations, I put

my function “hi” in HomeHelper and I call the method with <%= hi %> from my “home#index”, but give me an error:

I have searched in Google a solution or the explanation, but I don't

find. I am still thinking it might be happening…

Regards, Iván

please confirm that the code in the helper is like this

module HomeHelper

def hi

   if user_signed_in?

return “¡Bienvenid@ #{current_user.username} a nuestra web!”

else

return “¡Bienvenid@ invitad@, no dude en registrarse!”

end

end

end

in ruby there is no multi inheritance but you can import modules into classes, helpers are modules that are auto imported to he views of a given controller by rails , it appears you have deleted the module declaration inside the helper or you have placed the function outside the module declaration , in any case is if it still does not work please post the code in your helper.

Oh, thanks. I understand the code and work with the result that I want. Thanks again, Radhames.

With this problem I have learned things about the helpers and the

way to work with its and the controllers. Thanks also for the rest of the users who tried to help me.

Rewards, Ivan