Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - url_for behaving differently when using namespace (based on current controller being used)

lets assume namespace is "abc", we have a controller "abcs" and another one that uses namespace "abc" is "defs".

for easy understanding:

AbcsController
Abc::DefsController 

When current flow is in AbcsController, url_for({:controller => "abcs", :action => :new}) is returning correct url but when flow is in Abc::DefsController, when I am giving:

url_for({:controller => "abcs", :action => :new })

It is treating it as:

url_for({:controller => "abc/abcs", :action => :new})  #Observe abc/abcs

So, here it should be "abcs" but not "abc/abcs" but it is treating like that.

Whats the solution? Please ask me fr more information.

like image 997
user2139745 Avatar asked Nov 18 '13 21:11

user2139745


2 Answers

From here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

The last note box reads:

If you need to use a different controller namespace inside a namespace block you can specify an absolute controller path, e.g: get '/foo' => '/foo#index'.

This means all you have to do is this:

url_for({ controller: "/abcs", action: :new })

I ran into this problem as well and this solves it :)

like image 161
nzifnab Avatar answered Nov 05 '22 12:11

nzifnab


url_for also can take an array as an argument, you are probably looking for something like:

url_for([:edit, :admin, @product])

where the admin symbol would be your namespace.

like image 5
Graham Conzett Avatar answered Nov 05 '22 12:11

Graham Conzett