What’s form_for?  Plus a Bonus:  Why is Ruby called “Ruby” and
Why is Rails called “Rails?”

Tags: donnylobree, ruby, rails
Published: 2020-01-22 02:49:02


The form_with view helper is the new kid on the block.  It is meant to replace both form_for and the form_tag view helpers.  Both form_for and
form_tag are getting softly deprecated.

Let’s see what this is all about.

form_for

When creating a form from the information in our database, we’ll have a model which will serve as the container of the database’s information.  We’ll use this model to construct our form.  Say our model contained in the variable @article.  We’ll get:

<%= form_for @article do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>


form_tag
  But say we want to create a form but we don’t have a model.  For example building a contact form, integrating a search engine field, and pretty much every other aspect of an application that requires user input. 

The main difference between form_for and form_tag is that since we don’t have an object, we have to use urls to map out the form:

<%= form_tag “/posts” do %>
<%= text_field_tag “post[author]” %>
<%= submit_tag “Create” %>
<% end %>


 


Welcome Form_with

Form_with does away with having two form helps and combines their work into one.

<%= form_with model: @post do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>

<%= form_with url: “/posts” do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>


When Can we start using It?

It’s kind of like language.  When a new phrase or word comes into existence it may catch on or not.  As long as developers start using this new helper, form_tag and form_for will slowly get deprecated.




And now for a bit of fun.   Do you know how rails become to be known as rails?

Why did Matsumoto-san choose the name of Ruby?

He tells us:

“I gave a name of a jewel following Perl (a scripting language). I wanted it to do what Perl could do. There were candidates like sapphire and diamond but I turned them down because I felt typing up those names seemed a little complicated. Coral was also a candidate but I decided on ruby because ruby sounded more luxurious.”

He said he designed Ruby as the next Perl and there were interesting coincidences between the two.
“The birthstone for June is pearl, and ruby for July. Also, as old English type sizes, pearl is 5 point, and ruby is 5.5 point. ”
Considering those coincidences, Ruby was indeed next to Perl .

So that’s how Ruby got its name

But what about Rails?

Here’s how an anonymous developer described the emergence of rails:

“In the beginning there were computers and the people were happy
The users noted that these computers couldn’t talk to other computers
and they were sad
The network was thusly created and the people were happy
The frameworks that were initially developed were quite primitive and
the people were sad
Bigger frameworks were developed that allowed greater user experience
and productivity and the people were happy
As these frameworks grew bigger, and the complexity rose, developers
were given an increasing array of configuration options, development
strategies, financial advice, cosmic tea and other such things in which
to develop their particular applications which only lead to big slow
downs in things getting out the door and the people were sad
Along came 37Signals and DHH and he dictated that “convention over
configuration” shall be the new law, “Each of you will now develop my
way as if you were on rails” which greatly simplified web application
development and the people were happy…”


The story goes that the guys at 37signals(Later to become Basecamp) kept asking David Heinemeier Hansson, a Danish developer,  how he was getting
along with this widely unknown japanese scripting language that he had
decided to write their next web application in, and one day he
answered with a smile: “I put it (ruby) on rails”.

The point is rails is constraining.  That is good.

If you consider a train on rails, the train goes where the rails take
it. Ruby On Rails is the Ruby language on the “rails” that DHH
dreamed up. As the saying goes, RoR is very opinionated software.
You can do some things in a way that DHH and crew don’t like, but
they will make it hard for you. If you follow the Rails, all goes well.


Keep on Coding Your Dreams.  Namaste and blessings

Donny
















Keep on coding out your dreams.  Namaste and blessings,

Donny