My Ruby on Rails project cheatsheet: Stuff I’ve had to look up 100 times

Hello, World.

How to post code on Medium: Mac : command+option+6 | Windows : ctrl+alt+6 | Linux : ctrl+alt+6

Form_for template

<%= form_for @user do |f| %> 
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :age %>
<%= f.number_field :age %><br />
<%= f.label :password %>
<%= f.password_field :password %><br />
<%= f.submit %>
<% end %>

Flash message

In controller: flash[:alert] = "User not found."ORflash[:notice] = "Successfully logged in."In view: <% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>

Link_to

<%= link_to "text", "/add-link--here" %>OR replace with URL helpers in both spaces

Naming convention for many-to-many join tables

Migration table naming convention: groups_usersMigration file naming convention (the part of the file name that is not automatically generated): create_groups_usersModel class naming convention: GroupsUserModel file naming convention: groups_user.rb

Render partial

In view:<%= render 'author' %>Partial with local variables: <%= render partial: "authors/author", locals: {post_author: @post.author} %>
<%= @post.title %>
<%= @post.content %>

Routing

Shortcut to create all CRUD routes for one resource:

resources :users

Syntax to limit routes with resources:

resources :users, only: [:new, :create]

Validations

validates :name, :presence => true

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store