Ruby on Rails Project: Devhub

igmGuru
2 min readApr 29, 2022

Full Speed Ahead

Today we are building an app with complex relationships such as a many-to-many relationship with several other project specifications. This project management app was just the challenge I needed to solidify my knowledge in Ruby after finishing a full web application in the Ruby Sinatra framework a month ago. I have learned all this from ruby on rails training.

Off The Rails

Having just finished up with Sinatra, I really had to put in much more work to wrap my head around Rails. Sinatra was very lightweight and the barebones design is all you need for a simple app. Rails however is a very opinionated and useful framework to get large prototypes up and running fast. Learning was not as fast. I spent plenty of time discovering the Rails way and came to appreciate Convention over Configuration.

# devhub/app/views/projects/new.html.erb<%= form_for [@user, @project] do |form| %>
<%= render partial: "form", locals: {f: form} %>
<% end %>
# devhub/app/views/projects/_form.html.erb<div class = "container">
<%= f.hidden_field :user_id, :value => @user.id %>
<%= f.hidden_field :project_id, :value => @project.id %>
<strong><%= f.label :name %></strong>
<%= f.text_field :name, class:"form-control" %><br>
<strong><%= f.label :description %></strong>
<%= f.text_area :description, class:"form-control" %><br>
<strong><%= f.label :due_date %></strong>
<%= f.date_field :due_date, class:"form-control" %><br>
<%= f.submit 'Submit', class: "btn btn-primary" %>
</div>

Now we’re moving?

Whoa! that’s a lot of magic. The Rails documentation was my go-to for understanding all the built-in helpers that make Rails go. Rails’ follows the MVC architecture and this view was implicitly rendered from my Controller action:

# devhub/app/controllers/projects_controller.rbdef new
@project = Project.new
@user = current_user
end
# => Rails follows RESTful convention and assumes I need a view for this action based on the name **def new** and renders new.html.erb for me.

Well-Oiled Machine

Next, my new view template file takes advantage of more Rails magic, ERB(Embedded Ruby), and Rails Partials to dynamically render to totally different forms to submit.

# devhub/app/views/projects/new.html.erb<%= form_for [@user, @project] do |form| %>
<%= render partial: "form", locals: {f: form} %>
<% end %>
# => This code renders a form to create a new project!# devhub/app/views/projects/edit.html.erb<%= form_for [@user, @project] do |form| %>
<%= render partial: "form", locals: {f: form} %>
<% end %>
# => This code renders a form to edit a project!

Nuts & Bolts

These two identical snippets of code conditional will render a separate form based on conventional controller actions. Rails will do this for us: It recognizes def new to return the new project form and def edit to return the edit project form. These forms are glued together by this piece of code called a partial <%= render partial: "form", locals: {f: form} %> that will provide a two fully function forms for us.

--

--

igmGuru

We are an online IT training company delivering more than 150 training courses on all the niche technologies.