Some ways to DRY in Rails

1,094 views

Published on

Some ways to DRY in Rails

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,094
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Some ways to DRY in Rails

  1. 1. Seminar Some ways to DRY in Rails Presenter: Bui Trung Kien – Framgia Inc
  2. 2. Content ● Model ● Controller ● Views
  3. 3. Model ● Use 'scope' scope :admin, ->{where(admin: true)} ● Use 'module' app/model/concern/set_user.rb module SetUser //code goes here end app/model/user.rb class Admin < ActiveRecord::Base include SetUser
  4. 4. Controller ● Use 'before_action' app/controllers/users_controller.rb class UsersController < ApplicationController before_action :set_user, only: [:show, :edit, :update, :destroy] . . . private def set_user @user = User.find params[:id] end
  5. 5. Controller Use respond_with and respond_to class UsersController < ApplicationController respond_to :html, :json, :xml def index @users = User.all respond_with @users end
  6. 6. Controller ● Move business code logic into model methods. ● Move view logic code into helper methods. And then try to DRY it up.
  7. 7. View ● Use 'partial'. ● Use nested layout.
  8. 8. THANKS FOR WATCHING

×