Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create a Controller and Views to Manage Users

  • Loading branch information...
commit aa2b81ceeae1dfa1e0eb95b40a96e552fbb5576f 1 parent afc1fd2
@fortuity authored
View
230 README
@@ -225,6 +225,14 @@ and an initial SASS format stylesheet file:
public/stylesheets/sass/application.sass
+Modify the Application SASS Stylesheet
+
+Current Rails practice is to use "flash_notice" and "flash_alert" as the CSS identifiers for application messages. The SASS application stylesheet file generated by nifty_layout uses "flash_error" instead of "flash_alert". You'll need to change the file:
+
+public/stylesheets/sass/application.sass
+
+replacing "flash_error" with "flash_alert".
+
Add Stylesheets
You might wait until after you've built your application to begin applying CSS styling to your pages. Or you can do it now and your pages will look more visually appealing during development.
@@ -539,7 +547,7 @@ Sign in as "admin@test.com" with password "admin123". You'll see an error page b
Create a Controller and Views to Manage Administrators
-We want to view a list of administrators when we log in as an administrator. We also want to allow administrators to edit, delete, and add new administrators.
+We want to view a list of administrators when we log in as an administrator at the "admin" subdomain. We also want to allow administrators to edit, delete, and add new administrators.
Create a controller to manage administrators:
@@ -667,6 +675,226 @@ end
Now you can sign in as "admin@test.com" with password "admin123". You can "View List of Admins" and edit, delete, and add new administrators.
+Create a Controller and Views to Manage Users
+
+The site's home page has no subdomain. We have a home page for the app but we want to add a link to a page that shows a list of users. We want any visitor to be able to register as a user. We want any logged-in user to be able to edit or delete his or her own record (but no one else's).
+
+Create a controller to manage users:
+
+touch app/controllers/users_controller.rb
+
+with the following code:
+
+class UsersController < InheritedResources::Base
+
+ actions :index, :show, :new, :edit, :create, :update, :destroy
+ respond_to :html, :js, :xml, :json
+ before_filter :find_user, :only => [:edit, :update, :destroy]
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ if @user.respond_to?(:confirm!)
+ flash[:notice] = t('devise.confirmations.send_instructions')
+ # sign_in @user if @user.class.confirm_within > 0
+ else
+ flash[:notice] = t('flash.users.create.notice', :default => 'User was successfully created.')
+ end
+ redirect_to user_path(@user)
+ else
+ render :new
+ end
+ end
+
+ protected
+
+ def collection
+ paginate_options ||= {}
+ paginate_options[:page] ||= (params[:page] || 1)
+ paginate_options[:per_page] ||= (params[:per_page] || 20)
+ @users ||= end_of_association_chain.paginate(paginate_options)
+ end
+
+ def find_user
+ @user = User.find(params[:id])
+ unless current_user == @user
+ flash[:alert] = "You are not allowed to make changes to someone else's account."
+ redirect_to user_path(@user)
+ end
+ end
+end
+
+Create views to manage users:
+
+mkdir app/views/users/
+touch app/views/users/_form.html.haml
+touch app/views/users/_item.html.haml
+touch app/views/users/edit.html.haml
+touch app/views/users/index.html.haml
+touch app/views/users/new.html.haml
+touch app/views/users/show.html.haml
+
+Add the following code to each file:
+
+app/views/users/_form.html.haml
+
+- form.inputs do
+ = form.input :name, :label => 'Name'
+ = form.input :email, :label => 'Email'
+ = form.input :password, :label => 'Password'
+ = form.input :password_confirmation, :label => 'Password Confirmation'
+
+app/views/users/_item.html.haml
+
+- content_tag_for(:tr, user, :class => cycle(:odd, :even)) do
+ %td.name=link_to user.name, user_path(user)
+ %td.actions
+ = link_to 'Edit', edit_user_path(user)
+ |
+ = link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete
+
+app/views/users/edit.html.haml
+
+%h1.heading
+ = "Editing user %s" % @user.id
+
+- semantic_form_for(@user) do |form|
+ = render 'form', :form => form
+ - form.buttons do
+ = form.commit_button 'Update'
+
+%p.actions
+ = link_to 'Cancel', users_path
+
+app/views/users/index.html.haml
+
+%h1.heading
+ = "Users"
+
+%table
+ %thead.header
+ %tr
+ %th.name= 'Name'
+ %th.actions= 'Actions'
+ %tbody.items.users
+ - @users.each do |user|
+ = render 'item', :user => user
+
+= will_paginate(@users)
+
+app/views/users/new.html.haml
+
+%h1.heading
+ = 'New User'
+
+- semantic_form_for(@user) do |form|
+ = render 'form', :form => form
+ - form.buttons do
+ = form.commit_button 'Create'
+
+%p.actions
+ = link_to 'Cancel', users_path
+
+app/views/users/show.html.haml
+
+%h1.heading
+ = "User %s" % @user.id
+
+- content_tag_for(:dl, @user) do
+ %dt.label= 'Name'
+ %dd.name= h @user.try(:name)
+
+%p.actions
+ = link_to 'Edit', edit_user_path(@user)
+ |
+ = link_to 'List of Users', users_path
+
+Implement Routing for Users
+
+Add the following routes to implement this use case:
+
+map.subdomain nil do |main|
+ main.root :controller => "home"
+ main.resources :users
+end
+
+Add a Link to the Application Home Page
+
+We want a link to a list of users on the application home page.
+
+Modify the file:
+
+app/views/home/index.html.haml
+
+with these changes:
+
+- title 'Subdomain-Authentication'
+%p
+ This is my new app.
+= link_to "View List of Users", users_path
+
+Add Navigation Links to the Application Layout
+
+We want links to sign up, log in, etc. on each page of the application.
+
+Modify the file:
+
+app/views/layouts/application.html.haml
+
+with these changes:
+
+!!! Strict
+%html{html_attrs}
+
+ %head
+ %title
+ = h(yield(:title) || "Untitled")
+ %meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/
+ = stylesheet_link_tag 'application'
+ = yield(:head)
+
+ %body
+ #container
+ #navigation
+ - if user_signed_in?
+ = link_to "Home", root_path
+ |
+ = link_to 'My Account', user_path(current_user)
+ |
+ = link_to 'Sign out', destroy_user_session_path
+ - if admin_signed_in?
+ = link_to 'Admin Home', admin_root_path
+ |
+ = link_to 'Sign out', destroy_admin_session_path
+ - if !user_signed_in? && !admin_signed_in?
+ = link_to "Home", root_path
+ |
+ = link_to 'Register', new_user_path
+ |
+ = link_to 'Admin Login', admin_root_path
+ |
+ = link_to 'User Login', new_user_session_path
+
+ - flash.each do |name, msg|
+ = content_tag :div, msg, :id => "flash_#{name}"
+ #content
+ - if show_title?
+ %h1=h yield(:title)
+
+ = yield
+
+Test Sign Up
+
+If you launch the application and visit
+http://admin.localhost:3000/
+you can click a link to register as a new user. The app is configured to require a new user to confirm registration by clicking a link in an email message. The app's development environment is set up to log email messages instead of attempting to send them. Check your console or log file for a log entry that contains the text of the email message with the URL you can use to confirm the new user.
+
+It will look something like this:
+http://localhost:3000/users/confirmation?confirmation_token=b7iljFz77_3Sp6CftdFa
+
+Visit the confimation URL in your web browser to complete registration of a new user.
+
+
View
38 app/controllers/users_controller.rb
@@ -0,0 +1,38 @@
+class UsersController < InheritedResources::Base
+
+ actions :index, :show, :new, :edit, :create, :update, :destroy
+ respond_to :html, :js, :xml, :json
+ before_filter :find_user, :only => [:edit, :update, :destroy]
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ if @user.respond_to?(:confirm!)
+ flash[:notice] = t('devise.confirmations.send_instructions')
+ # sign_in @user if @user.class.confirm_within > 0
+ else
+ flash[:notice] = t('flash.users.create.notice', :default => 'User was successfully created.')
+ end
+ redirect_to user_path(@user)
+ else
+ render :new
+ end
+ end
+
+ protected
+
+ def collection
+ paginate_options ||= {}
+ paginate_options[:page] ||= (params[:page] || 1)
+ paginate_options[:per_page] ||= (params[:per_page] || 20)
+ @users ||= end_of_association_chain.paginate(paginate_options)
+ end
+
+ def find_user
+ @user = User.find(params[:id])
+ unless current_user == @user
+ flash[:alert] = "You are not allowed to make changes to someone else's account."
+ redirect_to user_path(@user)
+ end
+ end
+end
View
2  app/models/user.rb
@@ -6,5 +6,5 @@ class User < ActiveRecord::Base
devise :authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
- attr_accessible :email, :password, :password_confirmation
+ attr_accessible :name, :email, :password, :password_confirmation
end
View
3  app/views/home/index.html.haml
@@ -1 +1,4 @@
- title 'Subdomain-Authentication'
+%p
+ This is my new app.
+= link_to "View List of Users", users_path
View
38 app/views/layouts/application.html.haml
@@ -1,19 +1,39 @@
!!! Strict
%html{html_attrs}
-
+
%head
%title
= h(yield(:title) || "Untitled")
%meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}/
= stylesheet_link_tag 'application'
= yield(:head)
-
+
%body
#container
- - flash.each do |name, msg|
- = content_tag :div, msg, :id => "flash_#{name}"
-
- - if show_title?
- %h1=h yield(:title)
-
- = yield
+ #navigation
+ - if user_signed_in?
+ = link_to "Home", root_path
+ |
+ = link_to 'My Account', user_path(current_user)
+ |
+ = link_to 'Sign out', destroy_user_session_path
+ - if admin_signed_in?
+ = link_to 'Admin Home', admin_root_path
+ |
+ = link_to 'Sign out', destroy_admin_session_path
+ - if !user_signed_in? && !admin_signed_in?
+ = link_to "Home", root_path
+ |
+ = link_to 'Register', new_user_path
+ |
+ = link_to 'Admin Login', admin_root_path
+ |
+ = link_to 'User Login', new_user_session_path
+
+ - flash.each do |name, msg|
+ = content_tag :div, msg, :id => "flash_#{name}"
+ #content
+ - if show_title?
+ %h1=h yield(:title)
+
+ = yield
View
5 app/views/users/_form.html.haml
@@ -0,0 +1,5 @@
+- form.inputs do
+ = form.input :name, :label => 'Name'
+ = form.input :email, :label => 'Email'
+ = form.input :password, :label => 'Password'
+ = form.input :password_confirmation, :label => 'Password Confirmation'
View
6 app/views/users/_item.html.haml
@@ -0,0 +1,6 @@
+- content_tag_for(:tr, user, :class => cycle(:odd, :even)) do
+ %td.name=link_to user.name, user_path(user)
+ %td.actions
+ = link_to 'Edit', edit_user_path(user)
+ |
+ = link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete
View
10 app/views/users/edit.html.haml
@@ -0,0 +1,10 @@
+%h1.heading
+ = "Editing user %s" % @user.id
+
+- semantic_form_for(@user) do |form|
+ = render 'form', :form => form
+ - form.buttons do
+ = form.commit_button 'Update'
+
+%p.actions
+ = link_to 'Cancel', users_path
View
13 app/views/users/index.html.haml
@@ -0,0 +1,13 @@
+%h1.heading
+ = "Users"
+
+%table
+ %thead.header
+ %tr
+ %th.name= 'Name'
+ %th.actions= 'Actions'
+ %tbody.items.users
+ - @users.each do |user|
+ = render 'item', :user => user
+
+= will_paginate(@users)
View
10 app/views/users/new.html.haml
@@ -0,0 +1,10 @@
+%h1.heading
+ = 'New User'
+
+- semantic_form_for(@user) do |form|
+ = render 'form', :form => form
+ - form.buttons do
+ = form.commit_button 'Create'
+
+%p.actions
+ = link_to 'Cancel', users_path
View
11 app/views/users/show.html.haml
@@ -0,0 +1,11 @@
+%h1.heading
+ = "User %s" % @user.id
+
+- content_tag_for(:dl, @user) do
+ %dt.label= 'Name'
+ %dd.name= h @user.try(:name)
+
+%p.actions
+ = link_to 'Edit', edit_user_path(@user)
+ |
+ = link_to 'List of Users', users_path
View
5 config/routes.rb
@@ -3,6 +3,11 @@
map.devise_for :users
+ map.subdomain nil do |main|
+ main.root :controller => "home"
+ main.resources :users
+ end
+
map.subdomain :admin do |admin|
admin.root :controller => "AdminHome"
admin.resources :admins
View
4 public/stylesheets/application.css
@@ -137,7 +137,7 @@ a {
margin-top: 20px; }
#flash_notice,
-#flash_error {
+#flash_alert {
padding: 5px 8px;
margin: 10px 0; }
@@ -145,7 +145,7 @@ a {
background-color: #CFC;
border: solid 1px #6C6; }
-#flash_error {
+#flash_alert {
background-color: #FCC;
border: solid 1px #C66; }
View
4 public/stylesheets/sass/application.sass
@@ -29,7 +29,7 @@ a
:margin-top 20px
#flash_notice,
-#flash_error
+#flash_alert
:padding 5px 8px
:margin 10px 0
@@ -37,7 +37,7 @@ a
:background-color #CFC
:border solid 1px #6C6
-#flash_error
+#flash_alert
:background-color #FCC
:border solid 1px #C66
Please sign in to comment.
Something went wrong with that request. Please try again.