32

I know I can use request.referrer to get the full referrer URL in Rails, but is there a way to just get the controller name from the URL?

I want to see if the URL of http://myurl.com/profiles/2 includes "profiles"

I know I can use a regex to do it but I wondered if there was a better way.

2
  • it does not seem to be a right approach at all. What if some other server is the referrer?
    – Eru
    Commented Feb 2, 2013 at 19:20
  • 1
    Please @tvalent2, reselect the correct answer. The upvotes speak by themselves. Commented Mar 19, 2017 at 20:51

3 Answers 3

110

Keep in mind that request.referrer gives you the url of the request before the current one. That said, here is how you can convert request.referrer to controller/actionn information:

Rails.application.routes.recognize_path(request.referrer)

it should give you something like

{:subdomain => "", :controller => "x", :action => "y"}
1
  • 8
    Rails.application.routes.recognize_path(request.referrer)[:controller] to be precise. Commented Jan 17, 2015 at 22:28
4

Here is my try which works with Rails 3 & 4. This code extracts one parameter on logout and redirects user to customized login page otherwise it redirects to general login page. You can easily extract :controller this way. Controller part:

def logout
  auth_logout_user
  path = login_path
  begin
    refroute = Rails.application.routes.recognize_path(request.referer)
    path = subscriber_path(refroute[:sub_id]) if refroute && refroute[:sub_id]
  rescue ActionController::RoutingError
    #ignore
  end
  redirect_to path
end

And tests are important as well:

test "logout to subscriber entry page" do
  session[:uid] = users(:user1).id
  @request.env['HTTP_REFERER'] = "http://host/s/client1/p/xyzabc"
  get :logout
  assert_redirected_to subscriber_path('client1')
end

test "logout other referer" do
  session[:uid] = users(:user1).id
  @request.env['HTTP_REFERER'] = "http://anyhost/path/other"
  get :logout
  assert_redirected_to login_path
end

test "logout with bad referer" do
  session[:uid] = users(:user1).id
  @request.env['HTTP_REFERER'] = "badhost/path/other"
  get :logout
  assert_redirected_to login_path
end
1
  • woot woot for TDD on an answer! Commented Dec 9, 2014 at 21:30
-9

Inside the controller, you have the method controller_name which returns you only the name. In your case, it would return "profiles". You may also use params[:controller] which returns the same string.

2
  • 17
    Isn't this question incorrectly accepted? Did he not want to know how to decompose the action/controller of the referrer and not the current action's?
    – parreirat
    Commented Nov 28, 2014 at 9:24
  • I can delete my answer if it isn't correct but as far as i remember it worked perfectly
    – Erez Rabih
    Commented Sep 16, 2019 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.