Sunday, June 22, 2008

Rails: Modules & Routes

I used to make a module for every set of controllers which are doing certain task or feature because this way, things become organized in a better way

but something weird happened to me while trying to test a controller inside one of the modules, to see what happened i represent my case here by an example

i added in routes file this snippet of code

map.resources :items do |item|
item.resource :rate, :controller => 'ItemActions::Rate'
end

and in my controllers folder, made a folder called 'item_actions' and added inside it a file called 'rate_controller.rb', inside this file i wrote

module ItemActions
class RateController < ApplicationController
...
end
end

restarted the server and run my work, everything fine and i can call the controller actions as any other controller

then i opened the controller test file and inside one of its test functions, i added this code

post :create, {:item_id => 1, :rating => 50}


i got that error

No route to match {:controller => 'item_actions/rate', :action => 'create', :item_id => 1}


i changed the controller to be

class ItemActions::RateController < ApplicationController
...
end

but nothing changed, i am still getting that error once again, searching for a long time, i reached to a working solution which was to open routes file and change it to

map.resources :items do |item|
item.resource :rate, :controller => 'item_actions/rate'
end


That's it, it worked and i didn't get the error once again

No comments: