0

I'm new to RoR so maybe this question is quite wierd. In my code there are containers that have items in them, so routes.rb looks like:

  resources :containers, only: [:create, :index, :show] do
    resources :items, only: [:create, :index, :show] do

    end
  end

OK, I want to add a method for duplication of containers: duplicate. this method will get container ID and duplicate it and it's inner items, for example:

containers table:
-=-=-=-=-=-=-=-=-
id |c_name |details   |....
---+-------+----------+----
1  |Cont1  |Container1|....
2  |Cont2  |Container2|....

items table:
-=-=-=-=-=-=-
ID |i_name |container_id  |....
---+-------+--------------+----
1  |item777|1             |....
2  |item_Q3|1             |....
3  |item323|2             |....
4  |item_AA|2             |....

If I call the containers-duplicate method with param id=1 the result table will be:

containers table:
-=-=-=-=-=-=-=-=-
id |c_name |details   |....
---+-------+----------+----
1  |Cont1  |Container1|....
2  |Cont2  |Container2|....
3  |Cont1  |Container1|....

items table:
-=-=-=-=-=-=-
ID |i_name |container_id  |....
---+-------+--------------+----
1  |item777|1             |....
2  |item_Q3|1             |....
3  |item323|2             |....
4  |item_AA|2             |....
5  |item777|3             |....
6  |item_Q3|3             |....

what I understand is that I need to find the container by id and iterate over it's items and create a copy of them one by one. but my main problem is how the routing to this method is done?

The show method is automatically activated without writing "show" in the path (for example calling this path with get request will activate show method http://{{server}}:{{port}}/containers/{{containerId}}.json) , how can I add the duplicate route properly so calling the method will be done via http://{{server}}:{{port}}/containers/{{containerId}}/duplicate or something like this (small changes to the url can be done if nessesery) Plus, I want the duplicate to be a post request - where shuold I write it?

another thing, sometimes I want to duplicate Item inside a container - logic is quite the same as container duplication.

my question is: how the routing should be done? - what modifications should I do to routes.rb to allow duplication of containers and Items?

Im working with rubi version: ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32] and rails version:Rails 4.2.6

yossico
  • 3,421
  • 5
  • 41
  • 76

1 Answers1

2

Here are 3 options:

Option 1:

resources :containers, only: [:create, :index, :show] do

  post :duplicate, on: :member

  # Generated route:
  # duplicate_container POST   /containers/:id/duplicate(.:format)           containers#duplicate

  resources :items, only: [:create, :index, :show] do
  end
end

Option 2:

resources :containers, only: [:create, :index, :show] do

  resource :duplicate, only: [:create] 

  # Generated route:
  # container_duplicate POST   /containers/:container_id/duplicate(.:format) duplicates#create

  resources :items, only: [:create, :index, :show] do
  end
end

Option 3:

resources :containers, only: [:create, :index, :show] do

  resources :duplicate, only: [:create]

  # Generated route:
  # container_duplicate_index POST   /containers/:container_id/duplicate(.:format) duplicate#create

  resources :items, only: [:create, :index, :show] do
  end
end
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
  • Thanks for your answer, I don't understand why to call to create method - the duplicate is a different method -what am I missing? can you please add a stub metod in the controller for example? – yossico May 01 '16 at 14:53
  • I guess you want the first option. As you can see it is calling `duplicate` action/method on `ContainersController`. Is that want you wanted? – Uzbekjon May 01 '16 at 15:02