2. Part 1
• Type url and what’s next?
• Routing
• Controllers
• Views
• Models
3. Type url and what’s next?
rorapp.com/products/983
Routing
• get ‚products’ =>
‚products#index’
• get ‚products/new’ =>
‚products#new’
• post ‚products' =>
‚products#create’
• get ‚products/:id’ =>
‚products#show’!
• put ‚products/:id’ =>
‚products#update’
• delete ‚products/:id’ =>
‚products#destroy’
Controller
4. Type url and what’s next?
Controller
app/controllers/products_controller.rb
!
class ProductsController < ApplicactionController
Routing View
def show
@product = Product.find(params[:id])
end
!
end
Model
5. Type url and what’s next?
View
app/views/products/show.html.haml
!.
row
.col-sm-6
%h3= @product.name
.col-sm-6
%ul
- for category in @product.categories
%li= link_to category.name, category
Controller
6. Type url and what’s next?
View
app/views/products/show.html.haml
!.
row
.col-sm-6
%h3= @product.name
.col-sm-6
%ul
- for category in @product.categories
%li= link_to category.name, category_path(category)
Controller
7. Type url and what’s next?
Model
app/models/product_category_association.rb
app/models/product.rb
app/models/category.rb
class ProductCategoryAssociation < ActiveRecord::Base
belongs_to :category
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :product_category_associations
has_many :categories, :through
=> :product_category_associations
end Controller
class Category < ActiveRecord::Base
has_many :product_category_associations
has_many :products, :through
=> :product_category_associations
end
8. Type url and what’s next?
Model
class Product < ActiveRecord::Base
has_many :product_category_associations
has_many :categories, :through
=> :product_category_associations
end
Controller
class ActiveRecord::Base
!
def self.where […]
def self.find(id) […] object/exception
def self.order(params) […]
def self.new […]
!
def save […] => true/false
def save! […] => true/exception
def update_attributes […] true/false
def update_attributes! […] => true/exception
!
end
9. Routing
• get ‚products’ => ‚products#index’
• get ‚products/new’ => ‚products#new’
• post ‚products' => ‚products#create’
• get ‚products/:id’ => ‚products#show’
• put ‚products/:id’ => ‚products#update’
config/routes.rb
• delete ‚products/:id’ => ‚products#destroy’
11. Routing
namespace :admin do
resources :users
end
!
namespace :manager do
resources :products
end
!
namespace :customer do
resources :products do
collection do
get :bestsellers
=> customer/products/bestsellers
end
member do
post :add_to_cart
=> customer/products/433/add_to_cart
end
end
end
config/routes.rb