I am starting with rails and I am having a issue on nested routes with 2 ids.
I submit a form from:
http://localhost:3000/admin/worlds/1/banks/new
And I get redirect to:
http://localhost:3000/admin/worlds/23/banks/23
(with 23 being the id of the new bank)
The problem is that I can’t force this url to keep the first id = 1 as on the first route after the create action.
My Admin::Bank controller on the create here:
def create
@world_bank = Admin::Bank.new(params[:admin_banque])
@world_bank.world_id = params[:id]
@world_bank.save!
redirect_to admin_bank_path(@world_bank.world_id, @world_bank.id)
end
Routes:
namespace :admin do
resources :mondes do
member do
resources :banques
end
end
end
And rake routes:
admin_banks GET /admin/worlds/:id/banks(.:format) admin/banks#index
POST /admin/worlds/:id/banks(.:format) admin/banks#create
new_admin_bank GET /admin/worlds/:id/banks/new(.:format) admin/banks#new
edit_admin_bank GET /admin/worlds/:id/banks/:id/edit(.:format) admin/banks#edit
admin_bank GET /admin/worlds/:id/banks/:id(.:format) admin/banks#show
PUT /admin/worlds/:id/banks/:id(.:format) admin/banks#update
DELETE /admin/worlds/:id/banks/:id(.:format) admin/banks#destroy
The fact is that I see:
:id/thing/:id/stuff when I know I should get :world_it/thing/:id/stuff
I’ve tried different way to use the redirect, especially the redirect_to admin_banks(world_id, id) for example, but it stayed the same.
I am pretty sure this could be fixed with a match in the routes.rb but I can’t make it work for now…
Get rid of the :member block around your nested resource & it should work. Try: