Robs Reminders r3 – Authentication and Base model

r3 available via http://code.google.com/p/robsreminders/ is now up and running at http://robsreminders.com .

In this check-in I installed the authentication system, created the reminder model, added a site wide layout, and created a main controller. Wow! Most updates won’t be this big. Also the contents of this entry are very similar to my previous tutorial on act_as_authenticated.

The first step of course is to build the database and create authentication.

rake db:create

(Note: I had to change the socket: in config/database.yml on my ubuntu machine, I did this by generating a separate app and coping the config information.)

script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated

ruby script/generate authenticated user account

rake db:migrate

Add the AuthenticatedSystem to /app/controllers/application.rb

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time

include AuthenticatedSystem

The primary model for this application is the Reminder. Now I don’t have a complete design for the application, but this is what I have so far.

Reminder has title, body, due, complete.

script/generate scaffold Reminder title:string body:text done:boolean due:datetime

and of course tied to a user_id

script/generate migration add_user_id_to_reminder

class AddUserIdToReminder < ActiveRecord::Migration
def self.up

add_column :reminders, :user_id, :integer
end

def self.down
remove_column :reminders, :user_id
end
end

add the one to many relation between reminder and user

user.rb
has_may :reminders

reminder.rb
belongs_to :user

Create a main controller, this will serve as the root of the application. With and index method, and an index view.

Map the main controller to the root. routes.rb

map.root :controller => “main”

define an application wide template containing a login tool-bar.

/app/views/layouts/application.html.erb

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”content-type” content=”text/html;charset=UTF-8″ />
<title>Reminders: <%= controller.action_name %></title>
<%= stylesheet_link_tag ‘scaffold’ %>

</head>
<body>
<div style=”border-bottom:solid 1px black;”>

Robs Reminders
<span style=”float:right;”>
<% if current_user == :false %>
<%= link_to “Login”, {:controller => ‘account’, :action => ‘login’}%>
<%= link_to “Signup”, {:controller => ‘account’, :action => ‘signup’}%>
<% else %>
<%= current_user.login %>
<%= link_to “Logout”, {:controller => ‘account’, :action => ‘logout’}%>
<%= link_to “Edit URL”, {:controller => ‘main’, :action => ‘edit’}%>
<%= link_to “Reminders”, {:controller => ‘reminders’, :action => ‘index’}%>
<% end%>
</span>
</div>
<p style=”color: green”><%= flash[:notice] %></p>
<%= yield %>

</body>
</html>

delete the index.html page

Delete all other layout files

Modify the account controller to redirect to the main page.

Modify the reminders controller to user the current_user for loading and saving reminders. (as seen in the authentication tutorial. )
This takes us to SVN r3.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">