Upload
sagar-arlekar
View
221
Download
0
Embed Size (px)
Citation preview
8/7/2019 Getting Started with Rails Cheatsheet
1/5
Getting Started with Rails
Note: Working code for the Events App available here https://github.com/gnaroji/Events-
Application
Color codes used in this cheat sheet
command line
code
Create the app
rails events
cd events
Add Events
Scaffold for events
script/generate scaffold event title:string description:string start_date:date end_date:date
user_id:integer
Run the migrationrake db:migrate
Add Pagination to the Events
Install the plugin
script/plugin installgit://github.com/mislav/will_paginate.git
Modify Events Controller Code
(app/controllers/events_controller.rb indexmethod)
--------------------------------CODE-----------------------------
@events = Event.paginate(:per_page => 5, :page => params[:page], :order => 'updated_at
DESC', :conditions => ['start_date >= ?', Date.today])
--------------------------------CODE-----------------------------
Add Pagination controls to the Events Index page
(# app/views/events/index.html.erb)
--------------------------------CODE-----------------------------
--------------------------------CODE-----------------------------
Add Users
Scaffold for Users
script/generate scaffold user first_name:string last_name:string login:string email:string
persistence_token:string crypted_password:string password_salt:stringAssociate an event with a user
(# app/models/event.rb)
--------------------------------CODE-----------------------------
belongs_to :user
--------------------------------CODE-----------------------------
Add a user_id field to the events model
script/generate migration AddUsersToEvents user_id:integer
User Creation Form
http://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtwhttp://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fgnaroji%2FEvents-Application&sa=D&sntz=1&usg=AFQjCNEweHzL34TKLbyhnBwKsySbnwvMtw8/7/2019 Getting Started with Rails Cheatsheet
2/5
(# app/views/users/new.html.erb)
--------------------------------CODE-----------------------------
New user
--------------------------------CODE-----------------------------
Add User Authentication using Authlogic
Install Authlogic
script/plugin install git://github.com/binarylogic/authlogic.git
Add authentication to Users
(app/models/user.rb)
--------------------------------CODE-----------------------------
acts_as_authentic do |c|
c.require_password_confirmation = false
end
--------------------------------CODE-----------------------------
Create the model for User Session
script/generate session user_session
Create the controller for User Session
script/generate controller user_sessions new create destroy
Code forapp/controllers/user_sessions_controller.rb
--------------------------------CODE-----------------------------
8/7/2019 Getting Started with Rails Cheatsheet
3/5
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => :destroy
def new
@user_session = UserSession.new
end
def create@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "You have logged in successfully!"
redirect_to root_url
else
render :action => :new
end
end
def destroy
current_user_session.destroy
flash[:notice] = "You have logged out successfully!"
redirect_to root_url
end
--------------------------------CODE-----------------------------
View for login
(app/views/user_sessions/new.html.erb)
--------------------------------CODE-----------------------------
Sign In
user_session_path do |f| %>
--------------------------------CODE-----------------------------
Wire up the URLs
(config/routes.rb)
--------------------------------CODE-----------------------------
map.resource :user_session
map.root :controller => "events", :action => "index"
8/7/2019 Getting Started with Rails Cheatsheet
4/5
--------------------------------CODE-----------------------------
Application Controller code
--------------------------------CODE-----------------------------
helper_method :current_user_session, :current_user
filter_parameter_logging :password
privatedef current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
def require_user
unless current_user
flash[:notice] = "Please sign in to access this page"
redirect_to root_url
return false
end
end
def require_no_user
if current_user
flash[:notice] = "Please sign out to access this page"redirect_to root_url
return false
end
end
--------------------------------CODE-----------------------------
Only logged in users should be able to create events
(app/controllers/events_controller.rb add at the start of the newmethod)
-------------------------------CODE-----------------------------
if current_user.nil?
flash[:notice] = "You must be logged in to access this page"
redirect_to (new_user_session_url)
return
end
--------------------------------CODE-----------------------------
In case you have to use the Gem
# config/environment.rb
config.gem 'will_paginate', :version => '~> 2.3.11', :source => 'http://gemcutter.org'
8/7/2019 Getting Started with Rails Cheatsheet
5/5
config.gem "authlogic"