Skip to content

Instantly share code, notes, and snippets.

@Humoud
Last active March 16, 2016 08:47
Show Gist options
  • Save Humoud/53d54771f1fe0335d6d7 to your computer and use it in GitHub Desktop.
Save Humoud/53d54771f1fe0335d6d7 to your computer and use it in GitHub Desktop.
A mix of notes, conventions, and snippets for working with RoR.
# This URL was useful: https://gist.github.com/iangreenleaf/b206d09c587e8fc6399e
############################################################
################### ROUTES ####################
############################################################
get 'welcome/home', to: 'welcome#home' # HTTP GET /welcome/home to controller: welcome action(Method): home.
# Should have controller WelcomeController < ApplicationController
# controller should have action(Method) home
# should have a template (view) in app/views/welcome/home.html.erb
issue: `rake routes` in console:
will get: "welcome_home" as the prefix
namespace :admin do
get 'orders/reschedule', to: 'orders#reschedule_index', as: 'order_reschedule'
end
# Gives us the following url:
/admin/orders/reschedule(.:format)
# and will fire the following action(method):
spree/admin/orders#reschedule_index
############################################################
#########___|||||||| DB ||||||||||___#############
############################################################
Creating a One-to-Many relation:
-------- --------
| CAR | n <------- 1 | CLIENT |
-------- --------
Models:
class Car < ActiveRecord::Base
belongs_to :client
end
class Client < ActiveRecord::Base
has_many :cars
end
Migration File:
class CreateCarsAndClients < ActiveRecord::Migration
def change
create_table :cars do |t|
t.string 'model'
t.string 'company'
t.string 'year'
t.float 'price'
t.belongs_to :client, index: true # relation
t.timestamps null: false
end
create_table :clients do |t|
t.string 'name'
t.string 'civil_id'
t.timestamps null: false
end
end
end
#############################################################
#--//################# ERB #################//--#
#############################################################
# <%= link_to "link text", welcome_home_path %>
where welcome_home is a route perfix
############################################################
##############++++ Devise Gem ++++###############
############################################################
rails g devise MODEL; to create a model with devise features
You gain:
helper method: current_user
callbacks: before_action :authenticate_user!, except: [:index, :show]
>> Will redirect user to login if not logged in unless its at index or show
In ERB files:
# <% if user_signed_in? %>
HTML/ERB CODE
# <% end %>
# <% if post.user == current_user %>
HTML/ERB CODE
# <% end %>
############################################################
###############____ Conventions ____################
############################################################
Class names are CamelCase
Controller class names use CamelCase and have Controller as a suffix.
The Controller suffix is always singular (ie never use "controllers").
The name of the resource is usually plural.
Controller files go in app/controllers/#{resource_name}_controller.rb
So a toy controller is written: Class ToysController < ApplicationController
With File = app/controllers/toys_controller.rb
if big toys controller: app/controllers/big_toys_controller.rb
Methods and variables are snake_case
Database tables use snake_case.
Table names are plural.
Column names in the database use snake_case, but are generally singular.
Model class names use CamelCase.
Models are singular, and will map to the plural database table name.
Model attributes and methods use snake_case and match the column names in the database.
Model files go in app/models/#{singular_model_name}.rb
View file names, by default, match the controller and action that they are tied to.
Views go in app/views/#{resource_name}/#{action_name}.html.erb
#--- DB Relations ---#
Relations use snake_case and follow the type of relation,
so has_one and belongs_to are singular while has_many is plural.
Rails expects foreign keys in the database to have an _id suffix,
and will map relations to those keys automatically if the names line up.
CODE:
# app/models/bigfoot_sighting.rb
class BigfootSighting < ActiveRecord::Base
# This knows to use the profile_id field in the database
belongs_to :profile
end
# app/models/profile.rb
class Profile < ActiveRecord::Base
# This knows to look at the BigfootSighting class
# and find the foreign key in that table
has_many :bigfoot_sightings
end
#############################--|--##############################################
#/___________________\#
#################### END ######################################
####################======================######################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment