Model: provides the interface for stored data
Controller: bridge between model and views
Views: user interface (can be html, json, etc)
def index
message = "Hello, Rails!"
render plain: message
enddef index
message = "Hello, Rails!"
render html: "<h1>#{message}</h1>".html_safe
enddef get_query_string
render plain: "Query string parameters: #{params[:message]}"
enddef pass_data_to_views
@message="Hello, Rails"
end<h1>Accessing Instance Variables in Views</h1>
<div>
<p>Message: <%= @message %></p>
</div>def conditional_rendering
@data={
name: "John Doe",
country: "USA",
role: "admin",
is_logged_in: true
}
end<h1>Conditional Rendering</h1>
<table class="table">
<tr>
<td>Name</td>
</tr>
<tr>
<td><%= @data[:name] %></td>
<td><%= @data[:country] %></td>
<td><%= @data[:role] %></td>
</tr>
</table>
<% if @data[:is_logged_in] %>
<p>Welcome to the system</p>
<% else %>
<p>Please login to view the data</p>
<% end %> def looping_in_views
@users = [
{name: "John Doe", country: "USA", role: "admin"},
{name: "Jane Doe", country: "UK", role: "user"},
{name: "John Smith", country: "Australia", role: "user"}
]
end<h1>Looping in Views</h1>
<ul class="list-group">
<% @users.each do |user| %>
<li class="list-group-item">
<%= user[:name] %> is from <%= user[:country] %>
</li>
<% end %>
</ul><%= debug @users %><%= @users.inspect %>Execution will stop and dev server will show debug info.
Use c to continue to the next breakpoint.
Use break 71 to add a breakpoint on line 71.
debuggerInspect directly on the browser.
Use instance_variables or local_variables to list available variables.
consoleFormat and where to output can be configured on environments/<env>.rb
def logging_demo
Rails.logger.debug("The debug level is for detailed info that assists in development.")
Rails.logger.info("The info level provides information for the normal application process.")
Rails.logger.warn("The warning level indicates something unexpected that might not be an error.")
Rails.logger.error("The error level indicates that something went wrong.")
Rails.logger.fatal("The fatal level indicates that something went critically wrong.")
end def index
response = HTTParty.get(base_url)
@users = response.parsed_response
end
def show
response = HTTParty.get("#{base_url}/#{params[:id]}")
@user = response.parsed_response
end
private
def base_url
"https://fakestoreapi.com/users"
end def render_404
respond_to do |format|
format.html { render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found }
format.any { head :not_found }
end
end- all – All records
- find(id) – Find by ID, raises error if it does not exist.
- find_by(attr: val) – Find first match or nil if it does not exist.
- where(attr: val) – Filter records
- order(:attr) – Sort results
- limit(n) – Limit results
- offset(n) – Skip n records
- first / last – First/last record
- new(attr) – New object (not saved)
- create(attr) – New + save
- create!(attr) – New + save (raises error if invalid)
- save – Save record
- update(attr) – Update record
- find_or_create_by(attr) – Find or create
- destroy – Delete a record
- destroy_all – Delete multiple
- count – Number of records
- pluck(:attr) – Get column values
- group(:attr) – Group records˝
- having(cond) – Filter grouped records
- includes(:assoc) – Eager load (avoid N+1)
- joins(:assoc) – SQL JOIN
- select(:attr) – Select specific columns
- exists?(cond) – Check existence
- presence – Ensures a field is not empty.
- uniqueness – Ensures a field is unique.
- length – Limits character count.
- format – Validates with a regex.
- numericality – Ensures numerical value (e.g., integer, greater than).
- inclusion – Restricts values to a set.
- exclusion – Prevents certain values.
- acceptance – Requires agreement (e.g., terms of service).
- confirmation – Requires a matching field (e.g., password).
- custom validation – Uses validate :method_name.
before_action :authenticate_user! Devise auto-generates routes:
devise_for :usersGenerate customizable views:
rails g devise:viewsIn Controllers
class ArticlesController < ApplicationController
before_action :authenticate_user! # Ensure user is logged in
def index
@articles = current_user.articles # Fetch only current user's articles
end
endIn Views
<p>Welcome, <%= current_user.email %>!</p>In Helpers
module ApplicationHelper
def user_signed_in_message
"Hello, #{current_user.username}" if user_signed_in?
end
endIn Controllers
if user_signed_in?
puts "User is logged in as #{current_user.email}"
end<% if user_signed_in? %>
<p>Welcome, <%= current_user.username %>!</p>
<% else %>
<p>Please log in.</p>
<% end %>
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.admin?
can :manage, :all
else
can :read, Article
can :create, Article if user.persisted?
can :update, Article, user_id: user.id
end
end
endclass ArticlesController < ApplicationController
load_and_authorize_resource
def index
@articles = Article.accessible_by(current_ability)
end
endrescue_from CanCan::AccessDenied do |exception|
redirect_to root_path, alert: "Access denied!"
end