Rails routing in XML / JSON went crazy without looking

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

+4
source share
3 answers

If you have installed many polymorphs as a plugin, you should remove it from the provider / plugins, and not from environment.rb. Can you download rm -rf this plugin and try again.

0
source

Ok I gave up. He returned to the pre-variant โ€œhas many polymorphsโ€ and changed his changes each time, after each test he performed tests to make sure that it still works. After about an hour, and I think all / app / things are the same as when I ran into the problem. "has many polymorphs" as well as "active support", and the classes / models / views are correct (i.e. there is no HTML rendering and no views). And guess what; everything works perfectly! I donโ€™t know if this should make me happy or sad - in any case, I would still like to know what it was, what went wrong here ...

Js

PS Oh, and if I'm going to continue working with RoR, I will have to make SVN a priority. This is a complete necessity even for such a tiny project, because it seems that RoR breaks very easily and mysteriously (this is not the first 5-hour WTF that I had).

0
source

This time, it is best to restart the application from scratch. the rails are good.

-3
source

Source: https://habr.com/ru/post/1286105/


All Articles