views:

1290

answers:

3

I have a mystifying problem. In a very simple Ruby app i have three classes: Drivers, Jobs and Vehicles. All three classes only consist of Id and Name. All three classes have the same #index and #show methods and only render in JSON or XML (this is in fact true for all their CRUD methods, they are identical in everything but name). There are no views. For example:

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

The models are similarly minimalistic and only contain:

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 perform POST/create, GET/index and PUT/update on all three classes and GET/read used to work as well until I installed the "has many polymorphs" ActiveRecord plugin 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 cannot do a read any more. If i go to localhost:3000/drivers they all list nicely 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 a 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 is going on here? Why does the same code work for one class but not the other two? Why is it trying to do a #view on the ApplicationController?

I found that if I create a simple HTML view for each of the three classes these work fine. To each class I add:

format.html # show.html.erb

With this in place, going to localhost:3000/drivers/3 renders out the item in HTML and I get no errors in the log. But if attach .xml to the URL it again fails for two of the classes (with the same error message as before) while one will output XML as expected. Even stranger, on the two failing classes, when adding .js to the URL (to trigger JSON rendering) I get the HTML output instead!

Is it possible this has something to do with the "has many polymorphs" plugin? I have heard of people having routing issues after installing it. Removing "has many polymorphs" and "active support" from environment.rb (and rebooting the sever) seems to make no difference whatsoever. Yet my problems started after it was installed. I've spent a number of hours on this problem now and am starting to get a little desperate, Google turns up virtually no information which makes me suspect I must have missed something elementary. Any enlightenment or hint gratefully received!

JS

A: 

If you installed has many polymorphs as a plugin, you have to remove it from vendor/plugins not from environment.rb. Can you please rm -rf that plugin and try again.

Ryan Oberoi
Thanks for your reply. I have removed the has many polymorphs plugin but sadly no change.
John Schulze
and you restarted your server, right?
Ryan Oberoi
Absolutely, always do that just to be sure.
John Schulze
Ok. Here is what may be happening. Before you started down this path, you may have been using a different version of rails/active_support. I am not sure what you changed, but you are no longer hitting the respond_to loop in your code as expected. Search on google for why respond_to is not working as expected. This link may be helpful in debugging your situation: http://www.neeraj.name/blog/articles/612-rails-under-the-hood-how-respond_to-works
Ryan Oberoi
Yes, that sounds like a distinct possibility. Thanks for the link. I had a problem a couple of days ago with Rails versions 2.2.2 vs. 2.3.2 as well and was a little disappointed by the (seemingly) poor versioning in RoR. I would expect to get an error saying "incorrect version" or "requires ver. n.n.n" but no, I got "undefined method: CreateConnection" in ActiveRecord instead.
John Schulze
A: 

You best bet at this time may be to restart your app from scratch. rails is good like that.

Ryan Oberoi
You know what, that's exactly what I've resorted to. Will probably take a lot less time to reapply my changes one by one and testing after each one that it would trying to figure this out. Shame really, I was beginning to like RoR...
John Schulze
Please don't tell me I'm gonna end up awarding this "best answer"...
John Schulze
Maybe you can do a diff on the two things to really nail it down. Or better - just zip and send them to me. I am pretty curious on how the earlier version got hosed. Rails is fragile in that sense and Rails 2.3.* definitely has its quirks. I wish I could have been of more help, but as you would undoubtedly conclude, this situation was almost unhelpable from afar. :)Glad you got it working. Restarting from scratch is wildly faster in some situations with rails, especially with brand new projects like yours.
Ryan Oberoi
Yep. Up and running with SVN now so diagosing this kind of stuff should be easier in the future. And you're right, I didn't really have much hope in anyone being able to help. Thanks for your input though!
John Schulze
A: 

Ok. I gave up. Went back to a pre "has many polymorphs" version and appled my changes one by one, running tests after each one to make sure it was still working. About an hour later and I think all the /app/ stuff is the same as when I ran into trouble. "has many polymorphs" is re-installed and required along with "active support" and the classes/models/views are correct (i.e. no HTML render & no views). And guess what; it's all working perfectly! I don't know whether that should make me happy or sad - in any case I'd still really like to know what it was that went wrong here...

JS

P.S. Oh, and if I'm going to continue with RoR I will have to make SVN a priority. It's a total necessity even for such a tiny project since it seems RoR breaks very easily and mysteriously (this is not the first 5+ hour WTF I've had).

John Schulze
I have had that experience. Not just in rails. Maybe you can do a diff on the two things to really nail it down. Well, I guess my suggestion was indeed the best answer. Sorry :)
Ryan Oberoi