Forum: Ruby on Rails redirect_from BS ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bill W. (Guest)
on 2007-03-04 05:40
(Received via mailing list)
I've notice some entries in the log where folks are just poking around.
Is it possible to 'trap' these and redirect_to something valid like the
site's home page?

Thanks,
Bill
Al E. (Guest)
on 2007-03-04 15:30
Bill W. wrote:
> I've notice some entries in the log where folks are just poking around.
> Is it possible to 'trap' these and redirect_to something valid like the
> site's home page?

Sure. I'm working on a site right the requirements to visit a specified
page can be complex. I use something at the top of each method like

bad_action and return if ...

where the "if" clause includes a set of conditions anybody visiting a
specific page must meet (@user_logged_in, !params['id'].blank?, etc.)

bad_action simply redirects to the index page or, if the user is logged
in, to the  user home page.

--Al Evans
Bill W. (Guest)
on 2007-03-04 17:02
(Received via mailing list)
Hi Al,

Thanks for your reply.  More below.

Al Evans wrote:
> Bill W. wrote:
>> I've notice some entries in the log where folks are just poking around.
>> Is it possible to 'trap' these and redirect_to something valid like the
>> site's home page?
>
> Sure. I'm working on a site right the requirements to visit a specified
> page can be complex. I use something at the top of each method like
>
> bad_action and return if ...

I've got before_filters in place for that sort of thing.  What I was
thinking / wondering about is something at a higher level that would
catch
attempts to invoke controllers / actions that don't exist.  I guess I'm
asking 'is there some way to redirect to a valid page (like the site's
home
page) rather than just throwing a 404 'Page not found'?

I'm guessing I should have marked this [OT] now that I think about it.
It's
probably something that has to happen outside the Rails app.  I just
don't
know enough about the architecture to know where the 'trap' should be
set.
Where / what decides that a 404 error gets thrown and that it should
display
404.html?  Is it configurable so that I could set it to display my home
page
instead?

Thanks,
Bill
Al E. (Guest)
on 2007-03-04 20:14
Bill W. wrote:

> I've got before_filters in place for that sort of thing.  What I was
> thinking / wondering about is something at a higher level that would
> catch
> attempts to invoke controllers / actions that don't exist.  I guess I'm
> asking 'is there some way to redirect to a valid page (like the site's
> home
> page) rather than just throwing a 404 'Page not found'?

I think you can do...

map.connect '', :controller => "whatever", :action => "home"

...at the end of your routes and catch all unrecognized
controllers/actions. I haven't had occasion to do this, though. I don't
think it would work for the case where the controller is recognized, but
the action isn't.

--Al Evans
This topic is locked and can not be replied to.