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?
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
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.
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.
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.