Reputation: 23
I am trying to build a one page application and having trouble with Rails routing. Basically I want everything within admin route to admin controller index but json rails to specific resource. I've tried
namespace :admin do
constraints :format => 'html' do
match '*path' => 'admin#index'
end
constraints :format => 'json' do
resources :user, :items
end
end
In this case path will match greedily and match /admin/users.json If I move the :format => 'json' block up. It matches /admin/users
Looks like the constraints blocks I specify does not works at all.
Rails version 3.2.6 rake routes
/admin/*path(.:format) admin/admin#index {:format=>"html"}
admin_users GET /admin/users(.:format) admin/users#index {:format=>"json"}
/* other normal resources routes for admin users and admin items */
I have checked and there is no route /admin/users(.format) admin/users#index {:format=>"html"}
so looks like it is exactly what I think it would be. but somehow still does not works
Update: I have managed to get it workings if it move the json block up However if the html block is on top. It is still causing me problem. But I think it is good enough for me now. Thanks guys
The original problem is I used request.xhr? in the controller where I should have used respond_to
Update 2 Uhm not acutally working now when I go to /admin/users I got a Not acceptable error. Where I would think that the first rule wont be match and match the second rule.
Upvotes: 2
Views: 3743
Reputation: 770
Does it work if you move the json block up and make the format segment mandatory for each json resource? In Rails 3.2 this happens by setting the format option to true:
namespace :admin do
constraints(format: "json") do
resources :items, format: true
resources :users, format: true
end
constraints(format: "html") do
match "*path" => "admin#index"
end
end
Upvotes: 3