Domanda

i try to fill twice id in url, but when i send params twice id just one id fill the url id.

My route :

namespace :admin do
    resources :stores
    get "/:id/new_items"=> 'stores#new_items', as: :store_new_items
    post "/:id/create_items"=> 'stores#create_items', as: :store_create_items
    get "/:id/show_items/:id"=> 'stores#show_items', as: :store_show_items
    get "/:id/items/:id/new_items_sub" => 'stores#new_items_sub', as: :store_new_items_sub
    post "/:id/items/:id/create_items_sub" => 'stores#create_items_sub', as: :store_create_items_sub
    get "/:id/items/:id/show_items_sub/:id" => 'stores#show_items_sub', as: :store_show_items_sub
  end

my view :

<%= link_to "add new items", admin_store_new_items_sub_path(@store.id, @items.id), :class=> "btn" %>

i hope my url like this :

http://localhost:3000/admin/#{store.id}/items/#{items.id}/new_items_sub

but i get same id like this :

http://localhost:3000/admin/#{store.id}/items/#{store.id}/new_items_sub

please tell me when i'm wrong? thanks

È stato utile?

Soluzione

you have to create neseted routes for that .have a look at http://guides.rubyonrails.org/routing.html#nested-resources

for example

resources :publishers do
  resources :magazines do
    resources :photos
   end
end

will accept routes /publishers/1/magazines/2/photos/3

Altri suggerimenti

Your params should be unique, so you can't pass more than one different :id params. Instead. you can do something like:

get '/:store_id/show_items/:id', as: :store_show_items

and in view:

<%= link_to 'show items', store_show_items_path(@store.id, @item.id) %>

Also, you should read more about Resources and Nested Resources in Rails, there's probably no need to complicate your life by creating each route independently.

You could refactor this to use nested routes like this (you may have to change controller method names):

namespace :admin do
  resources :stores do
    resources :items, :only => [:new, :create, :show] do
      resources :subs, :only => [:new, :create, :show]
    end
  end
end

This would give you a few url helpers like this: new_store_item_sub_path(@store.id, @item.id) for the new action and store_item_sub_path(@store.id, @item.id, @sub.id) for the show action.

Run rake routes to see what helpers and routes you have access to.

Have a look here to find out more about nested routes.

Your code can be DRYed up significantly. Hopefully this works; might need some tweaking:

namespace :admin do
    resources :stores do

        member do 
            get :new_items, as: :store_new_items
            post :create_items, as: :store_create_items
        end

        get "show_items/:id"=> 'stores#show_items', as: :store_show_items

        resources :items do 
            get :new_items_stub => 'stores#new_items_sub', as: :store_new_items_sub
            post :create_items_stub => 'stores#create_items_sub', as: :store_create_items_sub
            get "show_items_sub/:id" => 'stores#show_items_sub', as: :store_show_items_sub
        end
     end
  end

Uses Member Routes (see 2.10) & Nested Resources


Nested Resources

The crux of your issue is that you're trying to pass the :id param twice

Fortunately, Rails has a solution to this, in the form of Nested Resources. These work by taking the "parent" id and prepending a singular prefix, such as :store_id, allowing you to use the :id param for another set of methods

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top