Let’s Form an idea and Present it to the World!

Justin Webb
6 min readOct 19, 2020

Welcome back to my not so weekly Software-Engineering boot-camp edition, blog! Firstly, I would like to apologize to my millions of fans, millions_of_fans = 0. Flatiron continues to be time-consuming and very challenging, so I have not been able to keep my promise of posting a blog every week. That said, I have created a log of my most significant challenges from each week and promise to post them ASAP. This post will focus on how to develop and locally render a form using Ruby on Rails. This blog will specifically cover a form’s syntax, placement inside of a Rails application, necessary controller components, and how to make a drop-down selection as a bonus. As my blogs currently have a maximum read-time, I will add informative resources to the bottom of this post! All clarifying comments/questions are appreciated and welcomed!

Popeye the Sailor Man

For those you out there scratching your heads and wondering, “What the heck is Ruby on Rails?” You can liken Rails to a can of spinach and Ruby to ‘Popeye the Sailor Man.’ Rails is commonly typed-casted as Ruby’s hotter, younger, computer programming language brother. When in fact, Rails is a loadable method library that fast-tracks the building of Ruby applications. Rails simplifies application building to the point that YOU can start making apps with less than two months of CS training, trust me. This blog focuses on creating and rendering a form to a ‘new.html.erb’ page. This means visitors of our local webpage can interact with our data and even submit valid instances to the database!

Ace the Bathound & Krypto the Superdog

For context, let’s say that we’re building an app that creates SuperPupper Partners for the Justice-League. Our app has three models, JusticeLeaguers, SuperPuppers, and a joiner model called Fusion. SuperPuppers and JusticeLeaguers can have many of each other through Fusion. Our database contains a list of all the Justice League members and known K-9 crime fighters. We will assume that all the needed associations and migrations were completed for us. Our mission, should we choose to accept it, is to create a ‘form’ page that allows visitors to partner an existing JusticeLeaguer with a SupperPupper and give the duo an alias.

Let’s start by outlining our FusionsController:

def index
#will show all created fusions
enddef show#will show a particular fusionenddef new#creates a new fusion instanceenddef create#will find or create a superpupper using parameters and build a valid fusion!!endprivate#as to hide from prying eyes
created by: The Simpsons
def fusion_params#sets the expected values of a new fusionend

Now we can fill in the controller with the necessary information. Surprisingly we start at the bottom!

privatedef fusion_params#sets the expected values of a new fusionparams.require(:fusion).permit(:superpupper_name, :justiceleaguer_id, :alias)end

We just told the computer to only accept fusion request forms that contain a super pupper name, a justice leaguer’s id, and a fusion alias. The code above deters people from inputting data that doesn’t align with the tables in our database. Now we’re all crime fighters!

Next, we develop our controller actions.

def index#access all existing fusions@fusions = Fusion.allenddef show#finds a particular fusion based upon their particular creation id@fusion = Fusion.find(params[:id])enddef new#creates a new fusion instance@fusion = Fusion.newenddef create#finds or creates a super pupper using the name provided by the usersuperpup = SuperPupper.find_or_create_by(name: fusion_params[:superpupper_name])#builds the objects according to the allowed parameters@fusion = superpupper.fusions.build(fusion_params)end
Created by: AdultSwim

TWIST: Just like any great movie or show, our mission to build a form has a twist. HQ added a validation to our fusion model that limits a duo’s alias to 12 characters! They want us to check if a form was valid and flash a success message if they follow the guidelines or a message containing their errors if they mess up. True to our noble nature, we accept their last-minute request with a clear and calculated head nod. After all, that’s easy for us! We go back to our create method and add:

I am the Night!
def create#finds or creates a super pupper using the name provided by the usersuperpup = SuperPupper.find_or_create_by(name: fusion_params[:superpupper_name])#builds the objects according to the allowed parameters@fusion = superpupper.fusions.build(fusion_params)#if they are allowed to save the form i.e. they have no errorsif @fusion.save#flash the success message and take them to see all fusionsflash[:success] = “It was Justice at first sight! Your duo was created.”redirect_to fusions_pathelse#if it does not save, show them all of the reasons why it was not valid in full sentences and let them try again.flash[:error] = @fusion.errors.full_messagesrender :newendend

We should also warn our visitors of the validation and expected arguments.

def new#creates a new fusion instance@fusion = Fusion.newflash.now[:notice] = “A warning friend. An alias has a 12 character maximum!”end

The only thing left to do is fill in our view pages.

<--- app/views/fusions/new.html.erb ---><h1>Got a fusion we should consider?</h1>#if they are sent back here after trying to make a fusion, show them each error they made<% if flash[:errors]%><% flash[:errors].each do |error| %><p><%= error %></p><% end %><% end %>#creates a form that users will use to create duos<%= form_for @fusion do |f| %>#find or create a super pupper<%= f.label :superpupper_name %><%= f.text_field :superpupper %>#input a fusion alias<%= f.label :alias %><%= f.text_field :alias %>#create a drop-down list of all the existing Justice Leaguers in the database for a user to chose from<%= f.collection_select :justiceleaguer_id, Justiceleaguer.all, :id, :name %><%= f.submit “Add them to Legion of Justice?”%><% end %>

Lastly we would need to add the a method that checks for a success message and displays one if it exist to our index page.

<--- app/views/fusions/index.html.erb ---><% if flash[:success]%><p><%= flash[:success]%></p><% end %>
gifycat Ultimate Flawless Victory

Job well-done team! I’d fight the tyranny of evil with you any day! Please be sure to refer to the links added for further guidance. I hope this was informative and that you enjoyed the read! As I am new to writing computer science blogs, I would greatly appreciate comments to strengthen my writing. Questions are also welcomed as they challenge my understanding of the material and will ultimately help both parties! Tune in the next blog to see if any of these fusions remain friends.

--

--

Justin Webb

I am a graduated environmental studies researcher, who began a computer science boot-camp. I will be blogging solutions to what I struggle with most.