Redirect in Method

18 views
Skip to first unread message

BeeRich33

unread,
Sep 27, 2017, 7:09:41 AM9/27/17
to sinatrarb
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

Mike Pastore

unread,
Sep 27, 2017, 6:19:20 PM9/27/17
to sinatrarb
On Wednesday, September 27, 2017 at 6:09:41 AM UTC-5, BeeRich33 wrote:
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 for more information.

BeeRich33

unread,
Sep 28, 2017, 12:43:43 PM9/28/17
to sinatrarb
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"

Mike Pastore

unread,
Sep 28, 2017, 1:51:22 PM9/28/17
to sinatrarb
On Thursday, September 28, 2017 at 11:43:43 AM UTC-5, BeeRich33 wrote:
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"

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


BeeRich33

unread,
Oct 4, 2017, 10:54:14 PM10/4/17
to sinatrarb
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.  


Mike Pastore

unread,
Oct 5, 2017, 12:09:47 AM10/5/17
to sinatrarb
On Wednesday, October 4, 2017 at 9:54:14 PM UTC-5, BeeRich33 wrote:

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.

BeeRich33

unread,
Oct 5, 2017, 6:41:03 AM10/5/17
to sinatrarb
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? 
Reply all
Reply to author
Forward
0 new messages