Discussion:
Redirect in Method
BeeRich33
2017-09-25 22:29:17 UTC
Permalink
Hi folks. I'm looking for a redirect operation to put into a method.

def rebounce(bounceURL)
redirect bounceURL
end

It seems the redirect is only used directly in routes, so I'm wondering it
is helpful anywhere. I have methods parked in a module. Any insight
appreciated.

Cheers
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Mike Pastore' via sinatrarb
2017-09-27 22:19:20 UTC
Permalink
Post by BeeRich33
It seems the redirect is only used directly in routes, so I'm wondering it
is helpful anywhere. I have methods parked in a module. Any insight
appreciated.
Load your module method(s) as helpers. Helpers called from a route (or
condition, or before/after filter, etc.) can invoke the built-in
redirect helper. See the Helpers section in the intro
<http://www.sinatrarb.com/intro.html> for more information.
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
BeeRich33
2017-09-28 16:43:43 UTC
Permalink
Thanks for the reply.

Already loaded as a helper module. Currently, the method is as follows:

def my_method.bounce(bounceURL)
redirect bounceURL
end


Comes up as an undefined method error:

"undefined method `redirect' for Motherlode:Module"
Post by 'Mike Pastore' via sinatrarb
Post by BeeRich33
It seems the redirect is only used directly in routes, so I'm wondering
it is helpful anywhere. I have methods parked in a module. Any insight
appreciated.
Load your module method(s) as helpers. Helpers called from a route (or
condition, or before/after filter, etc.) can invoke the built-in
redirect helper. See the Helpers section in the intro
<http://www.sinatrarb.com/intro.html> for more information.
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Mike Pastore' via sinatrarb
2017-09-28 17:51:22 UTC
Permalink
Post by BeeRich33
def my_method.bounce(bounceURL)
redirect bounceURL
end
"undefined method `redirect' for Motherlode:Module"
Something is fishy here. Why are you doing def my_method.bounce? That Ruby
syntax is usually reserved for (re)defining a method on an object that has
already been instantiated. In this case you are defining a method on the
my_method object (or if my_method is a method, the object returned by it).
I don't think it will be treated as a module instance method and it won't
get imported as a helper, just as a normal method hanging off another
object.

Here's a simpler example that demonstrates the correct way to do what
you're trying to do:

require 'sinatra'

module MyHelpers
def bounce
redirect '/foo'
end
end

helpers MyHelpers

get '/' do
bounce
end

$ curl -i http://localhost:4567
HTTP/1.1 302 Found
Content-Type: text/html;charset=utf-8
Location: http://localhost:4567/foo
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Content-Length: 0
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
BeeRich33
2017-10-05 02:54:14 UTC
Permalink
I'm not sure why my reply was not posted. I waited to see if it could get
approved, but nothing has shown up. So I reply again.

def Mymodule.bounce(bounceURL)
redirect bounceURL
end

I cleaned it up to post here and made a mistake.
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Mike Pastore' via sinatrarb
2017-10-05 04:09:47 UTC
Permalink
Post by BeeRich33
def Mymodule.bounce(bounceURL)
redirect bounceURL
end
That syntax is the equivalent of doing this:

module Mymodule
def self.bounce(bounceURL)
redirect bounceURL
end
end

Which is not the same as doing this:

module Mymodule
def bounce(bounceURL)
redirect bounceURL
end
end

If you have an existing module, you need to reopen it and add an instance
method, which is pretty easy to do in Ruby:

module Mymodule
end

module Mymodule
def bounce(bounceURL)
redirect bounceURL
end
end

Of course, there are other ways, but that's the easiest, non-hackiest way.
Here's a pretty gross way:

module Mymodule
end

Mymodule.send(:define_method, :bounce) do
redirect bounceURL
end

Hope that helps.
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
BeeRich33
2017-10-05 10:41:03 UTC
Permalink
OK, that preceeding Mymodule is confusing me:



What is the difference between having the module name in front of the
method name, and not having it present?
Post by 'Mike Pastore' via sinatrarb
Post by BeeRich33
def Mymodule.bounce(bounceURL)
redirect bounceURL
end
module Mymodule
def self.bounce(bounceURL)
redirect bounceURL
end
end
module Mymodule
def bounce(bounceURL)
redirect bounceURL
end
end
If you have an existing module, you need to reopen it and add an instance
module Mymodule
end
module Mymodule
def bounce(bounceURL)
redirect bounceURL
end
end
Of course, there are other ways, but that's the easiest, non-hackiest way.
module Mymodule
end
Mymodule.send(:define_method, :bounce) do
redirect bounceURL
end
Hope that helps.
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...