Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is url_for returning /assets for undefined routes?

I'm upgrading from rails 3.1.3 to 3.2.2, but for some reason now url_for always returns /assets if the route doesn't exist.

For example:

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

But I want it to to throw the normal ActionController::RoutingError as it normally does...

like image 739
bkempner Avatar asked Mar 28 '12 03:03

bkempner


People also ask

Should route Param numbers be relative to the location URL?

I have a custom Breadcrumb component that needs access to the global set of route params in order to render properly (swapping out the param numbers for their respective names). Having the params be relative is really counter-intuitive, especially since the location URL is available globally.

How does the routing in react-router work?

@lazarljubenovic The routing in react-router is hierarchical. Each <Route> evaluates the current location based on it's own props and propagates the result down the tree via context. In the above example, the outer route has no knowledge of the inner route.

Is this a typical error when reading undefined state?

It's not your typical error. If it were, you wouldn't have sworn under your breath. Cannot read property 'state' of undefined, no worries. You've seen that one plenty of times. This one was different. It wasn't even that the app crashed, it's deeper than that. You stare at your monitor through your fingers. That's it. That's all you get.

Where should route for invalid routing be placed?

Route for invalid routing should be placed at the last of all routes because routes are called in the order in which they are written. If we will write this route in the starting or in the middle somewhere then all routes which are written after this route will not work and will be redirected to be handled as the invalid route.


1 Answers

Rails does not check for route existence if you build up the route via specifying controller-action. And naturally it shows /assets for {} route.

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

url_for add_user_path

This ensures you will either succeed (for existent named route) or get an error.

HTH

like image 194
pokrovskyy Avatar answered Nov 15 '22 04:11

pokrovskyy