Seminar

Some ways to DRY in Rails

Presenter: Bui Trung Kien – Framgia Inc
Content
●

Model

●

Controller

●

Views
Model
●

Use 'scope'
scope :admin, ->{where(admin: true)}

●

Use 'module'
app/model/concern/set_user.rb
module SetUser
//...
Controller
●

Use 'before_action'
app/controllers/users_controller.rb
class UsersController < ApplicationController
before...
Controller
Use respond_with and respond_to

class UsersController <

ApplicationController

respond_to :html, :json, :xml
...
Controller
●

Move business code logic into model methods.

●

Move view logic code into helper methods.
And then try to D...
View
●

Use 'partial'.

●

Use nested layout.
THANKS FOR WATCHING
Upcoming SlideShare
Loading in...5
×

Some ways to DRY in Rails

779

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
779
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×