You are on page 1of 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 migration
rake db:migrate

Add Pagination to the Events


Install the plugin
script/plugin install git://github.com/mislav/will_paginate.git
Modify Events Controller Code
(app/controllers/events_controller.rb – index method)
--------------------------------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-----------------------------
<%= will_paginate %>
--------------------------------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:string
Associate 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


(# app/views/users/new.html.erb)
--------------------------------CODE-----------------------------
<h1>New user</h1>
<% form_for(@user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :username %><br />
<%= f.text_field :login %>
</p>
<p>
<%= f.label :password %><br />
<%=h f.password_field :password %>
</p>
<p>
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</p>
<p>
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</p>

<p>
<%= f.submit 'Create User' %>
</p>
<% end %>

<%= link_to 'Back', users_path %>


--------------------------------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 for app/controllers/user_sessions_controller.rb


--------------------------------CODE-----------------------------
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-----------------------------
<h1>Sign In</h1>

<% form_for @user_session, :url => user_session_path do |f| %>


<%= f.error_messages %>
<%= f.label :username %><br />
<%= f.text_field :login %><br />
<br />
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<br />
<%= f.check_box :remember_me %><%= f.label :remember_me %><br />
<br />
<%= f.submit "Sign In" %>
<% end %>
--------------------------------CODE-----------------------------

Wire up the URLs


(config/routes.rb)
--------------------------------CODE-----------------------------
map.resource :user_session
map.root :controller => "events", :action => "index"
--------------------------------CODE-----------------------------

Application Controller code


--------------------------------CODE-----------------------------
helper_method :current_user_session, :current_user
filter_parameter_logging :password

private
def 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 new method)
-------------------------------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'
config.gem "authlogic"

You might also like