In one of my helpers I would like to call a redirect_to(url) action. I
know the helpers are supposed almost only for formatting the view
purposes but it’s the best way I have found to do what I want. If I call
the redirect_to action in a helper I get the error:
undefined method `redirect_to’ for #<#Class:0x23aaedc:0x23aaeb4>
There are some hackish ways. Let’s start with the basics though:
What are you actually trying to do? Share that and someone can come up
with
the solution for you. If it’s a redirect, then it needs to be called
from a
controller. The best place to put that kinda stuff is the same place you
put
before_filters: in the controller itself as a protected method, or in
app/controllers/application.rb as a protected method if you want all
controllers to see it.
(protected doesn’t really make it protected in the Java sense, it just
makes it so it’s not a URL action, as all public methods in controllers
become actions, especially if you leave the default routes in).
There are some hackish ways. Let’s start with the basics though:
What are you actually trying to do? Share that and someone can come up
with
the solution for you. If it’s a redirect, then it needs to be called
from a
controller. The best place to put that kinda stuff is the same place you
put
before_filters: in the controller itself as a protected method, or in
app/controllers/application.rb as a protected method if you want all
controllers to see it.
(protected doesn’t really make it protected in the Java sense, it just
makes it so it’s not a URL action, as all public methods in controllers
become actions, especially if you leave the default routes in).
yes I tried to call a redirect from within a helper, but after thinking
twice I recognise that is a bad idea so I have rewritten the code to
call it from the controller.
Thank you anyway for the response.
Alvaro.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.