I have a mysterious problem. In a very simple Ruby application, I have three classes: drivers, workstations, and vehicles. All three classes consist only of Id and Name. All three classes have the same #index and #show methods and are only rendered in JSON or XML (this is actually true for all their CRUD methods, they are identical in everything except the name). No views. For instance:
def index @drivers= Driver.all respond_to do |format| format.js { render :json => @drivers} format.xml { render :xml => @drivers} end end def show @driver = Driver.find(params[:id]) respond_to do |format| format.js { render :json => @driver} format.xml { render :xml => @driver} end end
Models are similarly minimal and contain only:
class Driver< ActiveRecord::Base validates_presence_of :name end
In routes.rb, I have:
map.resources :drivers map.resources :jobs map.resources :vehicles map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'
I can do POST / create, GET / index and PUT / update in all three classes, and use GET / read to work until I install the ActiveLecord plugin "has many polymorphs" and added to environment.rb:
require File.join(File.dirname(__FILE__), 'boot') require 'has_many_polymorphs' require 'active_support'
Now for two of the three classes I can no longer read. If I go to localhost: 3000 / drivers , they all display fine in XML, but if I go to localhost: 3000 / drivers / 3 , I get an error:
Processing DriversController#show (for 127.0.0.1 at 2009-06-11 20:34:03) [GET] Parameters: {"id"=>"3"} [4;36;1mDriver Load (0.0ms)[0m [0;1mSELECT * FROM "drivers" WHERE ("drivers"."id" = 3) [0m ActionView::MissingTemplate (Missing template drivers/show.erb in view path app/views): app/controllers/drivers_controller.rb:14:in `show' ...etc
This is followed by another unexpected error:
Processing ApplicationController#show (for 127.0.0.1 at 2009-06-11 21:35:52)[GET] Parameters: {"id"=>"3"} NameError (uninitialized constant ApplicationController::AreaAccessDenied): ...etc
What's going on here? Why does the same code work for one class, but not for the other two? Why is he trying to make #view on ApplicationController?
I found that if I create a simple HTML view for each of the three classes, this works fine. For each class I add:
format.html # show.html.erb
At the same time finding localhost: 3000 / drivers / 3 displays the element in HTML, and I get no errors in the log. But if you attach .xml to the URL, it will fail again for the two classes (with the same error message as before), while one will output the XML as expected. Even a stranger, on two failed classes, when adding .js to a URL (to run JSON rendering), I get HTML output instead!
Is it possible that this has something to do with the plugin "has many polymorphs"? I heard about problems with routing after installation. Removing "has a lot of polymorphs" and "actively supporting" from environment.rb (and rebooting the server) seems to make no difference. However, my problems started after installing it. I have spent several hours on this problem now, and I'm starting a little desperate, Google almost does not receive information that makes me suspect that I must have missed something elementary. Any enlightenment or hint gratefully received!
Js