DEV Community

loading...

Ruby on Rails Project: Devhub

terrythreatt profile image Terry Threatt ・Updated on ・2 min read

Full Speed Ahead

I am rolling after submitting my most recent Ruby on Rails project for Flatiron Bootcamp. I was tasked with 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.

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>

Enter fullscreen mode Exit fullscreen mode

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.rb

def 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.

Enter fullscreen mode Exit fullscreen mode

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!

Enter fullscreen mode Exit fullscreen mode

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.

This is my stop

Thanks for taking the time to read about my journey learning more about Ruby on Rails. If you are interested in checking out my Rails web app, check out the link below and feel free to leave a comment about your experience learning Ruby on Rails.

Rails App - Github

Terry Threatt

Discussion (0)

pic
Editor guide