Best Practices

and Gems

Best Practices

are important.

Style

  • Don't use tab characters. Please. Use two spaces instead.

 

  • Use snake_case for method and variable names, CamelCase for class and module names.
  • Use space characters around operators and
    { braces }

 

  • test = "string interpolation"
    "Use #{test} when possible."

Which of these will Sam not cry about?

  1. a = "really"
    "This" + a + "sucks"
  2. b = {hello: "World"}
  3. VariableTwo = 1 * 2
  4. def some_method
      "foobar"
    end
    

Every file should have a purpose.

  • Put unused files out of their misery.

 

  • create.html.erb, user_fixtures.yml, etc.

 

  • Pay attention to generated files.

Routing

  • Technically, Rails allows you to define whatever routes you want.
  • But, how helpful is
    get "/users/hello", to: "AdminController#goodbye",
                        as: :foobar

Routing

  • Keep things as resourceful as possible!
resources :students
# GET       /students           students#index
# GET       /students/new       students#new
# POST      /students           students#create
# GET       /students/:id       students#show
# GET       /students/:id/edit  students#edit
# PATCH/PUT /students/:id       students#update
# DELETE    /students/:id       students#destroy

resources :students, only: [:show]
# GET       /students/:id       students#show

Routing

  • Use member and collection to define more actions
resources :emails do
  # POST   /emails/:id/archive   emails#archive
  member do
    post :archive
  end
  
  # GET    /emails/archived      emails#archived
  collection do
    get :archived
  end
end

Gems

Development

  • These will make development 10x better.

 

  • Will be included on all homeworks and projects from now on.

Development

  • pry + pry-byebug
    • Amazing debugging

 

  • quiet_assets
    • Better server logs

 

  • annotate
    • Annotates models
  • spring
    • Really fast commands

 

  • better_errors
    • Awesome error pages

 

  • guard
    • Watches for file changes

In action.

simple_form

<%= form_for @student do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :phone %>
  <%= f.text_field :phone %>

  <%= f.submit %>
<% end %>
<%= simple_form_for @student do |f| %>
  <%= f.input :name %>
  <%= f.input :email %>
  <%= f.input :phone %>
  <%= f.button :submit %>
<% end %>

slim-rails

<h1>Hey everyone!</h1>
<p>This is the <%= @somevar %> page.</p>

<%= simple_form_for @student do |f| %>
  <%= f.input :name %>
  <%= f.input :email %>
  <%= f.input :phone %>
  <%= f.button :submit %>
<% end %>

<div class="some-class" id="this-div">
  We can do this easily in Slim.
</div>

<% if 5 * 4 == 20 %>
  <div attr="yup">I can multiply.</div>
<% end %>
h1 Hey everyone!
p This is the #{@somevar} page.

= simple_form_for @student do |f|
  = f.input :name
  = f.input :email
  = f.input :phone
  = f.button :submit

.some-class#this-div
  | We can do this easily in Slim.

- if 5 * 4 == 20
  div attr="yup" I can multiply
home.html.erb
home.html.slim

Go forth

and be productive.