Rails routing to XML/JSON without views gone mad
Posted
by
John Schulze
on Stack Overflow
See other posts from Stack Overflow
or by John Schulze
Published on 2009-06-11T21:03:05Z
Indexed on
2012/11/10
11:02 UTC
Read the original article
Hit count: 346
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
© Stack Overflow or respective owner