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