Yes, again! I’m here to “brag” about my group project “ SlangTrans!”. The idea is to make a web application like wikipedia, where people could create new slangs and others could help to translate it into other languages. Since my teammate Fanzhong Zeng and I have limited language proficiency, we decided to limit language-options to: English, Mandarin and German.
As always, before we code, we planned our classes relationships as below:
After user signs in, user is able to create, edit, and delete a slang with definitions in different languages, and user could also relate books according to the slang. As guests, who don’t sign-up or log-in, they are only able to read the slangs, because of the authorization and authentication. It is basic and there are still more features we could add, but due to three-day-preparation, we limited our project content.
The most challenging part for me during our project, is nested form. Yes, that sounds-simple-but-a-lot-of-work-form. I’ll use Definitions belongs_to Slang relationship to explain how I accomplish it.
With creating a slang, definitions would also be created and saved in the database with the specific slang-id as shown in the following GIF.
Let’s start from the scratch.
Migration → Models → Controllers → Views
Because definitions belongs to slang, and we need to use Definition attributes in Slang, definition should have references instead of belongs_to to the Slang, where we have slang-id for definition. This will create a schema.rb as followed.
After migration, we need to make sure the relationship between slang and definition is also established in Slang model.
In Slang.rb, we used “accepts_nested_attributes_for :definitions, to make sure that user can submit attributes for new Definition instances in a form_for a Slang. You might wonder what “reject_if” is. Rejecting blank fields will ensure that Rails does not create a new Definition if any field of Definition is blank. We set a “:reject_if proc” to silently ignore any new record hashes if they fail to pass our criteria. Persistence is everything!
Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute…
How about Definition.rb? “references :slang” is created during migration. No other “trick” needed.
Migrations and models are set. Now the controller. Due to the form is in view/slangs/new, we need to create definition in slangs_controller.
Since we chose three different languages, and the creating form would show three fields for definitions, that’s why I did 3.times @slang.definitions.build. The “:id” is to make sure you do not end up with a whole lot of tasks. The “:_destroy”must be there so that we can delete tasks.
Now to views/slangs/new.erb file part.
“f.fields_for” in form_for connected Slang and Definition. Correct migration, proper model-set, precise controller content make sure that the submission of this form would work as intended.
Action View Form Helpers - Ruby on Rails Guides
Action View Form HelpersForms in web applications are an essential interface for user input. However, form markup can…
After all this done, our web application is able to do the creation as I shown in the previous GIF.
As an old saying: all Roads Lead to Rome, there must be a better way!
Smart and “lazy” developers created so many ways to nest a form. There are new gems that would make nested-form even easier. Like our dear friend Jakarai Forsythe recommended in his blog. Simple form gem.
Simplify Your Life with SimpleForm Gem
Tired of typing all those class sections, labels, and divs? Spending countless hours working building forms just to…
By Plataformatec. Rails forms made easy. Simple Form aims to be as flexible as possible while helping you with powerful…
And nested_form_fields gem.
This Rails gem helps creating forms for models with nested has_many associations. It uses jQuery to dynamically add and…