Forum: Ruby on Rails Basic Authentication

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.
david salisbury (Guest)
on 2007-05-30 03:34
I'm trying to blend an old ( and I mean old :) legacy system with a new
rails side.  The old system used basic authentication, and in attempting
to create a seamless integration between the two, rails will need to use
basic authentication also.  Problem is, we're not just wanting to
protect a few pages, but if they've logged in to carry that state around
with the pages.  With basic authentication, the only way to do this is
by using ( adjusting ) the url.  So we need to have two urls to the same
page, but one will check for authentication and one wont.

Since in rails a different url means a different method, right now ( and
I hate to do this and will look into all suggestions you guys might
have! ) I have two methods for each rails page, each rendering the same
view, one is protected and one isn't.  But what I need is a way for the
view to know which method called it, and based on that to adjust any
link_to'es accordingly.  So I might have a "list" method, and an "rlist"
method, and the view would need to see that "rlist" called it and adjust
it's links to say "redit" and "rshow" instead of "edit" and "show".

If there's a routes.rb solution which could get rid of the dual methods
I'm *all* ears, but seems the view is still going to need to detect
which method called it.

Thanks for any ideas and info!!  Oh yea.. and I'm using a plugin for the
basic authentication in rails.

-Dave
Steve R. (Guest)
on 2007-06-01 17:10
What I would do is this: write a method in application_helper.rb called
my_link_to which calls link_to with the appropriate parameters depending
on the name of the action (which you can examine in
self.controller.action_name).

Something like this might work:

def my_link_to(name, options = {}, html_options = nil,
*parameters_for_method_reference)
  if self.controller.action_name =~ /^r/
    link_to('r' + name, options, html_options,
parameters_for_method_reference)
  else
    link_to(name, options, html_options,
parameters_for_method_reference)
  end
end

The replace your link_to calls in the view with my_link_to.

steve
This topic is locked and can not be replied to.