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')) %>
<table>
<tr>
<th>Name</th>
<th>Short Name</th>
<th>Description</th>
</tr>
<% for category in @categories -%>
<tr class='<%= cycle('row1', 'row2')%>'>
<td><%= h(category.name) -%></td>
<td><%= h(category.short_name) -%></td>
<td><%= h(category.description) -%></td>
<td><%= link_to('Edit', :action => 'list', :id => category) -%></
td>
<td><%= link_to('Delete', {:action => 'destroy', :id => category},
:confirm => 'Are you sure you want to remove this category?', :method
=> :delete) -%></td>
</tr>
<% end %>
</table>
<p><%= link_to('New Category', categories_url) %></p>
<% form_tag(params[:id].blank? ? {:action => 'create'} : {:action =>
'update', :id => @category}) do -%>
<%= error_messages_for 'category' -%>
<table>
<tr>
<th>Name</th>
<th>Short Name</th>
<th>Description</th>
</tr>
<tr>
<td><%= text_field(:category, :name, :size => 20)
-%></td>
<td><%= text_field(:category, :short_name, :size => 20)
-%></td>
<td><%= text_field(:category, :description, :size =>
40) -%></td>
<td><%= submit_tag(params[:id].blank? ? 'Create' :
'Update') -%></
td>
</tr>
</table>
<% end %>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---