0

I'm trying to use a custom route to go to /admin/home but it keeps giving me the error: undefined local variable or method 'home_admins_path' for #<#<Class:0x007f8272855808>:0x007f8272b9f298> when using = link_to 'Home', home_admins_path

When I run rake routes it appears that path is valid: home_admins_path GET /admins/home(.:format) admins#home

routes.rb

MyApp::Application.routes.draw do
  devise_for :admins

  get '/admins/home' => 'admins#home', as: :home_admins_path

  resources :admins

  root to: 'pages#home'
end

admins_controller.rb

class AdminsController < ApplicationController
  load_and_authorize_resource

  def home
    render "admins/home.html.haml"
  end
end
Eric Norcross
  • 4,177
  • 4
  • 28
  • 53
  • I hope this answer would also be helpful http://stackoverflow.com/questions/7053754/ruby-on-rails-routes/7054021#7054021 – rubish Aug 20 '13 at 03:20

2 Answers2

2

When you specify a named path using the as option, "_path" is appended by Rails to the name. So for your route:

get '/admins/home' => 'admins#home', as: :home_admins_path

then named route becomes home_admins_path_path.

So, to get home_admins_path named route you need to change it to:

get '/admins/home' => 'admins#home', as: :home_admins 

Another option would be to add this route as a collection within the resources :admin like follows:

resources :admins do 
  collection do 
    get '/home', action: :home, as: :home
  end
end

And this will also give you a named route home_admins_path.

vee
  • 38,255
  • 7
  • 74
  • 78
0

You don't have to specify home_admins_path in "as". _path is automatically added by rails, so for your problem you just have to write the route like this

get '/admins/home' => 'admins#home', as: :home_admins

It will be expanded as home_admins_path or home_admins_url by rails.

techvineet
  • 5,041
  • 2
  • 30
  • 28
  • This answer was flagged as low-quality due to it's length. Though I agree, unfortunately answers like this are permitted. However, aside from their permitted nature, I urge you to expand upon this and provide a detailed answer instead of just a one-liner. – jeremy Aug 20 '13 at 03:00