Avoid to write buggy code
Md. Tauhidul Islam
Software Engineer
Nascenia Ltd.
[MVC] Controller
Generally, the only types of logic you should have in your controller are:
- Session and cookie handling.
- Model selection.
- Request parameter management.
- Rendering/redirecting.
Fat model, skinny controller
[MVC] View
View templating engine: ERB
If you’re not careful, you can soon end up with a large file that is a mix of HTML and
Ruby code.
This is also an area that can lead to lots of repetition, leading to violations of DRY
(don’t repeat yourself) principles.
[MVC] View
require 'ostruct'
helper_method :current_user
def current_user
@current_user ||= User.find session[:user_id] if
session[:user_id]
if @current_user
@current_user
else
OpenStruct.new(name: 'Guest')
end
end
<h3>Welcome, <%= current_user.name -%></h3>
<h3>
Welcome,
<% if current_user %>
<%= current_user.name %>
<% else %>
Guest
<% end %>
</h3>
[MVC] View
A couple of additional recommended Rails best practices:
- Use view layouts and partials
- Use presenters/decorators
[MVC] Model
Minimize the logic in views and controllers.
The only place left in an MVC architecture to put all that logic would be in the
models, right?
not quite
[MVC] Model
The only logic that should remain in your model is:
- ActiveRecord configuration
- Simple mutation methods
- Access wrappers
- Sophisticated queries
[MVC] Model
So if the logic shouldn’t go in the views, and it shouldn’t go in the controllers, and it
shouldn’t go in the models, well then, where should it go?
POROs
[MVC] Helper classes
The Rails framework places an emphasis on the named components (i.e., model, view,
and controller) of an MVC framework.
But sometimes we might need methods that don’t seem to fit into any of the three.
Rails generators conveniently build a helper directory and a new helper class to go
with each new resource we create.
Concerns
Model and Controller concern
- Reduce size of model or controller?
- DRY model and controller?
Gems
Ruby and Rails are supported by a rich ecosystem of gems. This is great for building up
a complex application quickly.
But sometimes the number of gems in the application’s Gemfile is disproportionately
larger than the functionality provided.
Database migrations
rake db:migrate
rake db:schema:load
rake data:migrate
Checking sensitive information into repositories
config/secrets.yml
config/initializers/secret_token.rb
config/database.yml
application.yml
Thank You

More Related Content

PPTX
What is ASP.NET MVC
PPTX
ASP .NET MVC - best practices
PPTX
Introduction to ASP.NET MVC
PPT
Zend framework 02 - mvc
PPTX
Advance java session 13
PPTX
MVC & backbone.js
POT
Beginning In J2EE
PDF
Php and-mvc
What is ASP.NET MVC
ASP .NET MVC - best practices
Introduction to ASP.NET MVC
Zend framework 02 - mvc
Advance java session 13
MVC & backbone.js
Beginning In J2EE
Php and-mvc

What's hot (20)

PDF
Angular JS lecture
PPTX
Mvc presentation
ODP
A Good PHP Framework For Beginners Like Me!
PPTX
Creating Tomorrow’s Web Applications Using Today’s Technologies
PPTX
Asp.Net MVC3 - Basics
DOCX
Month 2 report
PPTX
Introduction To JavaScript Ajax
PDF
Webforms or MVC
TXT
New text document
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 2...
PPT
PDF
Dot net-course-curriculumn
PPT
Introduction To Asp.Net Mvc
PDF
ASP.Net | Sabin Saleem
PDF
Ajax and RJS
PPTX
Feature and Future of ASP.NET
PPTX
Frames and its components
PPTX
Advance java session 9
PDF
AJAX in ASP.NET
Angular JS lecture
Mvc presentation
A Good PHP Framework For Beginners Like Me!
Creating Tomorrow’s Web Applications Using Today’s Technologies
Asp.Net MVC3 - Basics
Month 2 report
Introduction To JavaScript Ajax
Webforms or MVC
New text document
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 2...
Dot net-course-curriculumn
Introduction To Asp.Net Mvc
ASP.Net | Sabin Saleem
Ajax and RJS
Feature and Future of ASP.NET
Frames and its components
Advance java session 9
AJAX in ASP.NET
Ad

Similar to RubyConf Bangladesh 2017 - Rails buggy code (20)

PPT
MVC Demystified: Essence of Ruby on Rails
PPTX
Asp.net c# MVC-5 Training-Day-1 of Day-9
PDF
Principles of MVC for Rails Developers
PDF
Action-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
PDF
IRJET- Lightweight MVC Framework in PHP
PDF
CG_CS25010_Lecture
PDF
Ruby on rails RAD
PDF
Action-Domain-Responder: A Refinement of MVC
PPT
Asp.net,mvc
PPTX
MVC Framework
PDF
Principles of MVC for PHP Developers
PPTX
PDF
PPTX
Thin Controllers Fat Models - How to Write Better Code
PDF
Crash Course HTML/Rails Slides
PPTX
Getting started with MVC 5 and Visual Studio 2013
PDF
Introducing Ruby/MVC/RoR
PPS
Introduction To Mvc
PPTX
Beginners' guide to Ruby on Rails
PDF
Beyond rails new
MVC Demystified: Essence of Ruby on Rails
Asp.net c# MVC-5 Training-Day-1 of Day-9
Principles of MVC for Rails Developers
Action-Domain-Responder: A Web-Specific Refinement of Model-View-Controller
IRJET- Lightweight MVC Framework in PHP
CG_CS25010_Lecture
Ruby on rails RAD
Action-Domain-Responder: A Refinement of MVC
Asp.net,mvc
MVC Framework
Principles of MVC for PHP Developers
Thin Controllers Fat Models - How to Write Better Code
Crash Course HTML/Rails Slides
Getting started with MVC 5 and Visual Studio 2013
Introducing Ruby/MVC/RoR
Introduction To Mvc
Beginners' guide to Ruby on Rails
Beyond rails new
Ad

More from Ruby Bangladesh (7)

PPTX
RubyConf Bangladesh 2017 - Introduction to Ruby on Rails
PPTX
RubyConf Bangladesh 2017 - Core Ruby: How it works
PPTX
RubyConf Bangladesh 2017 - Speed up your API/backend
PPTX
RubyConf Bangladesh 2017 - Craft beautiful code with Ruby DSL
PPTX
RubyConf Bangladesh 2017 - Which language should I choose
PPTX
RubyConf Bangladesh 2017 - Elixir for Rubyists
PPTX
Rubyconf Bangladesh 2017 - Lets start coding in Ruby
RubyConf Bangladesh 2017 - Introduction to Ruby on Rails
RubyConf Bangladesh 2017 - Core Ruby: How it works
RubyConf Bangladesh 2017 - Speed up your API/backend
RubyConf Bangladesh 2017 - Craft beautiful code with Ruby DSL
RubyConf Bangladesh 2017 - Which language should I choose
RubyConf Bangladesh 2017 - Elixir for Rubyists
Rubyconf Bangladesh 2017 - Lets start coding in Ruby

Recently uploaded (20)

PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PDF
Autodesk AutoCAD Crack Free Download 2025
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
DNT Brochure 2025 – ISV Solutions @ D365
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PPTX
Trending Python Topics for Data Visualization in 2025
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
Topaz Photo AI Crack New Download (Latest 2025)
PDF
Visual explanation of Dijkstra's Algorithm using Python
PDF
AI Guide for Business Growth - Arna Softech
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PPTX
CNN LeNet5 Architecture: Neural Networks
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PPTX
Computer Software - Technology and Livelihood Education
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
Autodesk AutoCAD Crack Free Download 2025
Salesforce Agentforce AI Implementation.pdf
DNT Brochure 2025 – ISV Solutions @ D365
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
How Tridens DevSecOps Ensures Compliance, Security, and Agility
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
Trending Python Topics for Data Visualization in 2025
Computer Software and OS of computer science of grade 11.pptx
iTop VPN Crack Latest Version Full Key 2025
Topaz Photo AI Crack New Download (Latest 2025)
Visual explanation of Dijkstra's Algorithm using Python
AI Guide for Business Growth - Arna Softech
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Monitoring Stack: Grafana, Loki & Promtail
CNN LeNet5 Architecture: Neural Networks
Why Generative AI is the Future of Content, Code & Creativity?
Computer Software - Technology and Livelihood Education

RubyConf Bangladesh 2017 - Rails buggy code

  • 1. Avoid to write buggy code Md. Tauhidul Islam Software Engineer Nascenia Ltd.
  • 2. [MVC] Controller Generally, the only types of logic you should have in your controller are: - Session and cookie handling. - Model selection. - Request parameter management. - Rendering/redirecting. Fat model, skinny controller
  • 3. [MVC] View View templating engine: ERB If you’re not careful, you can soon end up with a large file that is a mix of HTML and Ruby code. This is also an area that can lead to lots of repetition, leading to violations of DRY (don’t repeat yourself) principles.
  • 4. [MVC] View require 'ostruct' helper_method :current_user def current_user @current_user ||= User.find session[:user_id] if session[:user_id] if @current_user @current_user else OpenStruct.new(name: 'Guest') end end <h3>Welcome, <%= current_user.name -%></h3> <h3> Welcome, <% if current_user %> <%= current_user.name %> <% else %> Guest <% end %> </h3>
  • 5. [MVC] View A couple of additional recommended Rails best practices: - Use view layouts and partials - Use presenters/decorators
  • 6. [MVC] Model Minimize the logic in views and controllers. The only place left in an MVC architecture to put all that logic would be in the models, right? not quite
  • 7. [MVC] Model The only logic that should remain in your model is: - ActiveRecord configuration - Simple mutation methods - Access wrappers - Sophisticated queries
  • 8. [MVC] Model So if the logic shouldn’t go in the views, and it shouldn’t go in the controllers, and it shouldn’t go in the models, well then, where should it go? POROs
  • 9. [MVC] Helper classes The Rails framework places an emphasis on the named components (i.e., model, view, and controller) of an MVC framework. But sometimes we might need methods that don’t seem to fit into any of the three. Rails generators conveniently build a helper directory and a new helper class to go with each new resource we create.
  • 10. Concerns Model and Controller concern - Reduce size of model or controller? - DRY model and controller?
  • 11. Gems Ruby and Rails are supported by a rich ecosystem of gems. This is great for building up a complex application quickly. But sometimes the number of gems in the application’s Gemfile is disproportionately larger than the functionality provided.
  • 12. Database migrations rake db:migrate rake db:schema:load rake data:migrate
  • 13. Checking sensitive information into repositories config/secrets.yml config/initializers/secret_token.rb config/database.yml application.yml

Editor's Notes

  • #3: Session and cookie handling. This might also include authentication/authorization or any additional cookie processing you need to do. Model selection. Logic for finding the right model object given the parameters passed in from the request. Ideally this should be a call to a single find method setting an instance variable to be used later to render the response. Request parameter management. Gathering request parameters and calling an appropriate model method to persist them. Rendering/redirecting. Rendering the result (html, xml, json, etc.) or redirecting, as appropriate.
  • #6: Use view layouts and partials appropriately to encapsulate things that are repeated on your pages. Use presenters/decorators like the Draper gem to encapsulate view-building logic in a Ruby object. You can then add methods into this object to perform logical operations that you might otherwise have put into your view code.
  • #8: ActiveRecord configuration (i.e., relations and validations) Simple mutation methods to encapsulate updating a handful of attributes and saving them in the database Access wrappers to hide internal model information (e.g., a full_name method that combines first_name and last_name fields in the database) Sophisticated queries (i.e., that are more complex than a simple find); generally speaking, you should never use the where method, or any other query-building methods like it, outside of the model class itself
  • #12: Makes the size of a Rails process larger than it needs to be Slow down performance in production Need for larger server memory configurations and increased operating costs Takes longer to start larger Rails applications (which makes development slower and makes automated tests take longer)