Hello, I am attempting to implement the “Clean Up Controllers with
Postback Actions” Rails Recipe and I am having a problem with it.
First, the code:
== Controller Method ==
def add_product_type
puts “Params: ‘#{params[:id]}’”
puts “PT: #{@product_type.id}” unless @product_type.nil?
@product_type = ProductType.find_by_id(params[:id]) ||
ProductType.new
puts “PRODUCT TYPE: ‘#{@product_type.name}’ -
‘#{@product_type.id}’”
if request.post?
@product_type.attributes = params[:product_type]
begin
if @product_type.save!
@product_type = nil
redirect_to :action => :index and return
end
@product_type = nil
rescue ActiveRecord::RecordInvalid => e
end
end
end
== View ==
<%= start_form_tag :action => ‘add_product_type’ %>
Name: <%= text_field 'product_type', 'name' %>
<% if @product_type.id.nil? %> <%= submit_tag "Add" %> <% else %> <%= submit_tag "Edit" %> <% end %><%= end_form_tag %>
So, I can submit a new type and it works flawlessly. If I immediately
return to the add a product page, it fills in the last product type’s
name. The output from the above puts’s:
Params: ‘’
PRODUCT TYPE: ‘’ - ‘’
127.0.0.1 - - [19/Sep/2006:18:22:39 EDT] “POST /admin/add_product_type
HTTP/1.1” 302 93
http://localhost:3000/admin/add_product_type → /admin/add_product_type
127.0.0.1 - - [19/Sep/2006:18:22:39 EDT] “GET /admin HTTP/1.1” 200 4569
http://localhost:3000/admin/add_product_type → /admin
Params: ‘’
PRODUCT TYPE: ‘gg’ - ‘16’
<--------------------------------------------- ???
127.0.0.1 - - [19/Sep/2006:18:22:42 EDT] “GET /admin/add_product_type
HTTP/1.1” 200 1053
http://localhost:3000/admin → /admin/add_product_type
The params are nil and yet it is finding a product type? Am I doing
something incorrectly here?
I am running the latest rails out of trunk as of today and have the
issue when using webrick, lighttpd and mongrel.
Thanks in advance for any help. I’m not certain where to begin with
this one.