Ruby on Rails, Flight Attendants & error partials w/ locals

Allison Poiré
2 min readMay 19, 2021

So, I am a flight attendant and also a student at Flatiron School.

For my most recent project, I was tasked with using Ruby on Rails and I chose to create a restaurant guide. Specifically, this restaurant guide allows my user to create a city and/or a restaurant/s and provide details as well as entering their personal recommendations.

Something that really simplified one aspect of my project, was super helpful, and seemed unnecessarily complex prior to implementing it was using an error partial. A simple way to explain partials is moving redundant (repeated) lines of the same code from multiple views and placing the code into an _error partial (also found in the views files and is in fact preceded by the “_” if this seemed like a typo, it is not). From there, in each view where you need to display errors you can direct to the partial. This is cleaner visually and, if my project were to become far larger in scale (think facebook) partials make things much easier to both maintain and implement changes.

Here is what I did in my project and I’ll walk through how I created my error partial and a few other details specific to what I was doing in my project using my recommendation model as the example. My goal is to put this as simply as possible so if you're lost and trying to do something similar I hope it helps.

First, because I am displaying errors for all of my models I located my error partial in my layouts views.

The code looks like this:

<% object.errors.full_messages.each do |error_message|%>

<%= error_message %>

<% end %>

What this is saying, in simple terms, is for every error related to the object let me know what they are in a full message.

Cool. So if I am using the recommendation model as my example why does it say object?

Well, this partial isn’t just for Recommendation. It is also for City and Restaurant and any other model for which it would be the best option. Standby for more on this.

So from here we need to communicate in the view where it should look for errors. This is my new recommendation form:

<h1> What do you recommend?! </h1>

<%= form_with model: [@restaurant, @recommendation], local: true do |f| %>

<%= render partial: ‘layouts/error’, locals: {object: @recommendation}%>

<p>

<%= f.label :try %>

<%= f.text_field :try %><br>

<div>

</div>

<%= f.label :avoid%>

<%= f.text_field :avoid%>

<div>

</div>

<%= f.submit %>

</p>

<% end %>

Here you see in the form where it indicates that I am using a partial and where it is located. In the same line you’ll see the locals… what is happening here is we are setting a variable. So, when the user submits input that doesn’t pass my validations and the partial is called on it knows what is being passed in as the Object is this instance of @recommendation (or @restaurant, @city)!

WAY simpler than it seemed in my mind (why I overcomplicate things I couldn’t say) and such better practice.

--

--