Why does url_for return / assets for undefined routes?

I am upgrading from rails 3.1.3 to 3.2.2, but for some reason url_for always returns / assets if the route does not exist.

For instance:

url_for({}) #=> "/assets" url_for({action: 'fake', controller: 'notreal'}) #=> /assets?action=fake&controller=notreal 

But I want him to run the usual ActionController :: RoutingError, as it usually happens ...

+6
source share
2 answers

Rails does not check for a route if you create a route by specifying the action of the controller. And, of course, it shows / assets for the {} route.

You better specify named routes in routes.rb and then use them for url_for. How:

url_for add_user_path

This ensures that you either succeed (for the existing named route) or receive an error message.

NTN

+1
source

I think you have a precompilation of assets, and since the image does not exist in the resource folder, the name of the compilation file is null, but the path points to the root of the resource folder. Do you also see an error in not finding the precompiled asset in the logs?

Try running a precompilation of resources in a development environment to get past this. I will not redraw the details of the preliminary compilation of assets - you can check http://guides.rubyonrails.org/asset_pipeline.html for detailed information on the asset pipeline. If you see this problem only in a production environment, it may be because the host platform pre-combines the assets for you.

However, if you expect this to happen in production, you can check for an image instead of disabling precompilation.

0
source

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


All Articles