One of my favorite resources in Rails has become the routes information page, which is accessible under your "localhost," followed by "/rails/info/routes." I have yet to find a better, more technical term for this page, so moving forward the "Rails Routes Page" is what I will be referencing it as. I have just completed a project built with Ruby on Rails, and its primary focus for a user is being able to leaves reviews of various sites. For the purpose of my application, the reviews can be left for any zoos in the country, including the ability for a user to add a new zoo to the database that may not have been reviewed yet. A zoo is associated with a U.S. state which has to be done on initialization, which is where the Rails Routes Page comes in incredibly handy!
For the purpose of this post, I'm going to highlight an example of using a nested route in my recent Rails project, and how the Rails Routes Page helped me reach the solution.
In the context of my application "Zoo Reviews," a logged-in user has access to a full list of states, and clicking on any given state will route the user to that specific state's list of zoos available already to be reviewed. Each state's show page actually routes the user to the zoos index page, where the conditionals have been set accordingly; if the url contains a location's ID integer, then the zoos index page is only reflective of the zoos for that specific location, otherwise all zoos across all states are listed. This means that the route to the zoos index page has a nested component, seeing as the url would contain something like "/locations/5/zoos." When a user lands on a page like this, there is a link available to add a new zoo to the database, but rather than choosing from a dropdown menu of state options, that zoo-location association is already being built. This is based on a variety of factors, so please don't think that these are all of the necessary steps or actions to building a nested route, rather I am looking to showcase the importance of using the Rails Routes Page as a resource.
So, let's say the user has navigated to the state of California's list of zoos ("/locations/5/zoos"). Now, this user has also clicked the link to add a new zoo to California's list of zoos. Here is part of what is happening behind the scenes:
In order to choose the correct route here to ensure that it is in fact nested and acknowledging that automatic association with the specific state/location, I had to refer to the Rails Routes Page. It goes without saying that as any developer builds on their skills, the needs to refer to this page may diminish but especially with more complex associations, having a full list of routes that are available to use as well as the necessary arguments certain routes require is one less thing to worry about.
So, in this instance, as a developer I knew we are 1) adding a new zoo, 2) associating it with a location for the user, and 3) also allowing the ability for the user to add a review by way of a nested form. To get to this nested form for a new zoo and review, we have to direct the application to "zoos/new." To assure it is still associated with the location on creation, we have to use the route dedicated to this action:
Though small in the midst of dozens of other routes, this is the route required to carry out this action. Here, we can see that this route accepts one argument, which is the location's specific ID, which we can also see being implemented in my code above. In all honesty this was eye-opening for me because just by its name, this does not sound like the appropriate route; sounds to me like it would be adding a new location. But the BEST part about the Rails Routes Page is that it lists the controller action in the last column all the way to the right. Here, Rails is indicating that this route actually takes us to zoos#new. This is exactly what I needed here!
The application continues on to not only build the zoo-location relationship, but again allows the user to add a review with the zoo upon initialization. It is important to know that only the review becomes associated with the current-user, not the zoo or location.
Another important aspect that the Rails Routes Page covers that I still struggle with is when to pluralize a model name, and when not to, in the context of a route. In this example, nothing is pluralized regardless of the fact that a location "has many" zoos in this application. As a student developer I have gotten increasingly familiar with names largely being pluralized outside of the model itself in a "has many" relationship, in general terms. However, in this route that is not the case and I am led to believe the Rails Routes Page saved me a great deal of wondering, debugging, and vexation by clearly spelling these things out for me, and that's what makes this a highly valuable resource, too.
Top comments (0)