I started out with a normal scaffold for the USERS table. I changed
the index view to be list.html.erb but the url: ‘/users/list’ returns
an ‘unknown action’ error. ‘/users/list/:id’ also returns an ‘unknown
action’ error. I’ve also merged both the edit and new views into the
manage.html.erb which duplicates the list view except with a form on
the right side. the url ‘/users/manage’ returns an ‘unknown action’
error but ‘/users/manage/:id’ brings me to the manage page with the
edit form.
in my routes.rb, it reads:
map.resources :users
The update an delete actions work but they don’t route me to the right
url. the new action does not work. can anyone help? I’ve pasted my
controller and views below.
USERS CONTOLLER
class UsersController < ApplicationController
layout ‘staff’
def index
list
render :action => ‘list’
end
GET /users
GET /users.xml
def list
@users = User.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end
GET /users/new
GET /users/new.xml
def manage
list
@user = User.find(params[:id]) if params[:id]
@user = User.new if @user.nil?
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end
POST /users
POST /users.xml
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to(users_url) }
format.xml { render :xml => @user, :status
=> :created, :location => @user }
else
format.html { render(:action => “manage”) }
format.xml { render :xml => @user.errors, :status
=> :unprocessable_entity }
end
end
end
PUT /users/1
PUT /users/1.xml
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
format.html { redirect_to(users_url) }
format.xml { head :ok }
else
format.html { render(:action => 'manage') }
format.xml { render :xml => @user.errors, :status
=> :unprocessable_entity }
end
end
end
DELETE /users/1
DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
flash[:notice] = 'User was successfully deleted.'
format.html { redirect_to(:action => 'list') }
format.xml { head :ok }
end
end
end
LIST VIEW
<% @page_title = ‘Current Users’ -%>
<%= content_tag(‘p’, link_to(’« Back to Menu’, :controller =>
‘staff’, :action => ‘menu’)) %>
MANAGE VIEW
<% @page_title = ‘Current Users’ -%>
<%= content_tag(‘p’, link_to(’« Back to Menu’, :controller =>
‘staff’, :action => ‘menu’)) %>
New user
<% form_for(@user) do |f| -%>
<%= render(:partial => 'form') -%>
<p class= 'user_submit'><%= submit_tag "Create" -%></p>
<% end -%>
<% else -%>
Editing user
<% form_for(@user) do |f| -%>
<%= render(:partial => 'form')%>
<p class= 'user_submit'><%= submit_tag "Update" -%></p>
<% end -%>
<p class= 'user_submit'><%= link_to('Delete User', { :action =>
‘destroy’,
:id => @user }, :confirm => ‘Are you sure you want to permanently
delete this user?’,
:method => :delete) -%>
<% end -%>
<%= link_to(‘Cancel’, :action => ‘list’) %>
_LISTING PARTIAL
First Name | Last Name | Username | User Level | |
---|---|---|---|---|
<%= h(user.first_name) -%> | <%= h(user.last_name) -%> | <%= h(user.username) -%> | <%= h(user.user_level) -%> | <%= link_to('Edit', :action => 'manage', :id => user) -%> |
_FORM PARTIAL
<%= error_messages_for(:user) -%>
Username | <%= text_field(:user, :username) -%> |
---|---|
Hashed Password | <%= text_field(:user, :hashed_password) -%> |
First Name | <%= text_field(:user, :first_name) -%> |
Last Name | <%= text_field(:user, :last_name) -%> |
<%= text_field(:user, :email) -%> | |
Display Name | <%= text_field(:user, :display_name) -%> |
User Level | <%= select(:user, :user_level, [0,1,2,3,4,9]) -%> |