It does make sense to put them in one action. I think that this is better than the standard scaffold-generated code:
Code:
class ProductsController < ApplicationController
def index
list
render :action => 'list'
end
def list
@products = Product.find_all
end
def show
load_product
end
def new
save_product
end
def edit
load_product
save_product
end
def destroy
load_product
@product.destroy
redirect_to :action => 'list'
end
protected
def save_product
if @request.post?
@product.attributes = params[:product]
if @product.save
redirect_to :action => 'show', :id => @product
end
end
end
def load_product
@product = Product.find(params[:id])
end
end
Your form will look like this:
Code:
<% form_for :product, Product.new do |p| %>
<p>Name:<br />
<%= p.text_field :name %></p>
<p>Description:<br />
<%= p.text_area :description %></p>
<p>Save:<br />
<%= submit_tag 'Save' %></p>
<% end %>
Bookmarks