Archive for December, 2007

Rob’s Reminders r7

Major:

Added a new action to the reminders controller.

I added a new method and view to my reminders controller to show completed items.

In Reminders Controller I added

def completed
@reminders = Reminder.find(:all, :conditions => [ "done = ? and user_id = ?", true, current_user.id])
end

I also added completed.html.erb to the views for reminders. However when I accessed the /reminders/completed url I got an error message.

ActiveRecord::RecordNotFound in RemindersController#show

WTF? Why is the error in show? Well after a half hour or cursing rails I figured it out.

In the routes.rb file you will see a line

map.resources :reminders

What this does is add mappings for index, edit, create, show. With everything other then index, edit, create assigned to show.

You have to add a mapping for your new method to get around this.

map.connect 'reminders/completed', :controller => 'reminders', :action => 'completed'
map.resources :reminders

Now you can try the completed method, and it will work.

Minor changes:

Changed title, added Calendar control to created reminders.

Also robsreminders.com is finally up and running. Thanks to dream host. They are now running rails 2.0.2, and where a ton of help. I recommend them if you are looking for a rails host.

Comments

Robs Reminders r4

Robs Reminders r4

Implement remember me.

I had forgotten to uncomment the remember me block of code in the /app/views/account/login.rhtml file. Removing this will allow users to skip the login process once they have successfully logged in once.

Change the date/time control

The current date time control is lousy. Fortunately there is a plugin that vastly improves it.

http://code.google.com/p/calendardateselect/

script/plugin install http://calendardateselect.googlecode.com/svn/tags/calendar_date_select

Add the following to the application.html.erb layout

<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>
  <%=  javascript_include_tag ‘prototype’ %>
<%= calendar_date_select_includes nil %>

Edit the /app/views/reminders/edit.html.erb file

Change

<%= f.datetime_select :due %>

to

<%= f.calendar_date_select :due, :embedded => true, :time => true %>

A restart of the app server is required after updating.

Comments

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.

Comments

Robs Reminders

When I’m learning a new programming language or framework its important for me to have a project to build. Abstract test cases are nice, but for me it needs to be something that is real. Something that I can and would use.

For rails I’ve deiced to make a todo list / reminder application. And for a twist I’ve also thought it would be fun to build this application out in the open.

I’ve purchased a domain name robsremiders.com, and have also setup a public repository at http://code.google.com/p/robsreminders/. Please feel free to run your own copy, and submit patches back.

Everything I put on my production website will be via the public repository. And I will blog about every application change I make.

robsreminders.com -r2 Check-in of rails generated project.

Comments

Todo List using Rails 2.0.1 with ‘act as authenticated’

I’ve been looking at Rails examples for well over a year now. I’ve seen a lot of todo list examples, forum examples, and others. The one thing missing from these examples is authentication. I mean what kind of web app does not have authentication? Logging in / logging out and having other records attached to your user ID is a must for any real webapp.

In this tutorial I’ll show you how to build a todo list app, with authentication.

rails realtodo
cd realtodo
rake db:create

The act as authenticated plugin must be downloaded and installed first.

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

More information on this plugin can be found here. http://wiki.rubyonrails.org/rails/pages/Acts_as_authenticated

Now using the new plugin generated the authentication

ruby script/generate authenticated user account

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

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

And now our todo list. generated just like before.

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

We also need to add the user_id column to the Todo, we don’t do this in scaffolding so that we don’t have to removed user_id from all of the generated forms.

script/generate migration add_user_id_to_todo

edit the generated file /db/migrate/003_add_user_id_to_todo.rb

class AddUserIdToTodo < ActiveRecord::Migration
def self.up
add_column :todos, :user_id, :integer
end

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

modify the todo.rb and user.rb files. We need to add the one to many relation

/app/models/todo.rb

class Todo < ActiveRecord::Base
belongs_to :user
end

/app/models/user.rb (Lots of stuff in this generated class, just add has_many :todos

class User < ActiveRecord::Base
# Virtual attribute for the unencrypted password
attr_accessor :pass
has_many :todos

Change the todo controller so that login is required by adding
near the top of the controller.

/app/controllers/todos_controller.rb

class TodosController < ApplicationController
before_filter :login_required

act_as_authenticated adds the curent_user object everywhere. This is great because we can just use it in our controller. We only want to show todo’s for each user, and other users should  not be able to see other users todos. To do this we need to change how todo are loaded and saved.

def index
#@todos = Todo.find(:all)
@todos = current_user.todos

def show
#@todo = Todo.find(params[:id])
@todo = current_user.todos.find(params[:id])

def new
#@todo = Todo.new
@todo = current_user.todos.create

def edit
#@todo = Todo.find(params[:id])
@todo = current_user.todos.find(params[:id])

def create
#@todo = Todo.new(params[:todo])
@todo = current_user.todos.create(params[:todo])

def update
#@todo = Todo.find(params[:id])
@todo = current_user.todos.find(params[:id])

def destroy
@todo = current_user.todos.find(params[:id])
@todo.destroy

With the controller created we now need an index page to link everything together.

delete the /public/index.html file

generate a new controller for the main page

script/generate controller Main

Define and index function on the main controller

  def index
end

and create a new view under views/main/index.rhtml

Todo lists

<% if current_user == :false %>
<%= link_to "Login",   {:controller => ‘account’, :action => ‘login’}%>

<%= link_to "Signup",  {:controller => ‘account’, :action => ’signup’}%>
<% else %>
You are logged in as <%= current_user.login %>

<%= link_to "Todos", {:controller => ‘todos’, :action => ‘index’}%>

<%= link_to "Logout", {:controller => ‘account’, :action => ‘logout’}%>
<% end%>

Edit the account controller to return to the main index page by default

change every redirect_back_or_default(:controller => ‘/account’, :action => ‘index’) to
redirect_back_or_default(:controller => ‘/main’, :action => ‘index’)

add the following line to /config/routes.rb

 map.root :controller => “main”

from the command line
rake db:migrate
script/server

Thats it. Navigation between the todo lists and the index needs to be added, but we now have a complete working rails todo list webapp, with seperate todo lists for each user.

Comments (2)

« Previous entries