i’m having a routing problem with my UPDATE action. I’ve pasted my
CATEGORIES controller and view below. I ran scaffold for categories
and i consolidated everything into a single view so that I could show
list, create new, edit, destroy all from a single page. my create,
destroy actions work and my index view works as expected. my UPDATE
action however returns the following:
Processing CategoriesController#16 (for 127.0.0.1 at 2008-09-08
13:08:22) [POST]
Session ID: c05797629988686fd3951de29fca7e24
Parameters: {“commit”=>“Update”,
“category”=>{“name”=>“Entertainment”, “description”=>“Latest gossip
about your favourite stars12”, “short_name”=>“entertainment”},
“authenticity_token”=>“b6a61d7dc0890d83a923366cd5c93e672b16f62e”,
“action”=>“16”, “controller”=>“categories”}
and it identifies the object id as my action and redirects me to /
categories/:id. it should redirect me to simply /categories with a
flash notice saying my object has been updated. i should also note
that it also doesn’t save the updated entry to my database.
I’m running rails 2.1 and I haven’t changed my default routes in
routes.rb
Thanks for your help.
HERE’S MY CONTROLLER
class CategoriesController < ApplicationController
layout ‘staff’
#verify :method => :post, :only => [ :destroy, :create, :update ],
#:redirect_to => { :action => :list }
GET /categories
GET /categories.xml
def index
list
render :action => ‘list’
end
def list
@categories = Category.find(:all)
@category = Category.find(params[:id]) if params[:id]
@category = Category.new if @category.nil?
#respond_to do |format|
#format.html # index.html.erb
#format.xml { render :xml => @categories }
#end
end
POST /categories
POST /categories.xml
def create
@category = Category.new(params[:category])
respond_to do |format|
if @category.save
flash[:notice] = 'Category was successfully created.'
format.html { redirect_to categories_url }
format.xml { render :xml => @category, :status
=> :created, :location => @category }
else
format.html { render categories_url }
format.xml { render :xml => @category.errors, :status
=> :unprocessable_entity }
end
end
end
PUT /categories/1
PUT /categories/1.xml
def update
@category = Category.find(params[:id])
respond_to do |format|
if @category.update_attributes(params[:category])
flash[:notice] = 'Category was successfully updated.'
format.html { redirect_to categories_url }
format.xml { render :xml => @category, :status
=> :created, :location => @category }
else
format.html { render categories_url }
format.xml { render :xml => @category.errors, :status
=> :unprocessable_entity }
end
end
end
DELETE /categories/1
DELETE /categories/1.xml
def destroy
@category = Category.find(params[:id])
@category.destroy
respond_to do |format|
flash[:notice] = 'Category was successfully removed.'
format.html { redirect_to categories_url }
format.xml { head :ok }
end
end
end
HERE’S MY VIEW --> categories/list.html.erb
<% @page_title = ‘Categories’ -%>
<%= content_tag(‘p’, link_to(’« Back to Menu’, :controller =>
‘staff’, :action => ‘menu’)) %>
Name | Short Name | Description | ||
---|---|---|---|---|
<%= h(category.name) -%> | <%= h(category.short_name) -%> | <%= h(category.description) -%> | <%= link_to('Edit', :action => 'list', :id => category) -%> | <%= link_to('Delete', {:action => 'destroy', :id => category}, :confirm => 'Are you sure you want to remove this category?', :method => :delete) -%> |
<%= link_to('New Category', categories_url) %>
<% form_tag(params[:id].blank? ? {:action => ‘create’} : {:action =>
‘update’, :id => @category}) do -%>
<%= error_messages_for ‘category’ -%>
Name | Short Name | Description | |
---|---|---|---|
<%= text_field(:category, :name, :size => 20) -%> | <%= text_field(:category, :short_name, :size => 20) -%> | <%= text_field(:category, :description, :size => 40) -%> | <%= submit_tag(params[:id].blank? ? 'Create' : 'Update') -%> |