| Class | AdminController |
| In: |
app/controllers/admin_controller.rb
|
| Parent: | ApplicationController |
The administration functions allow authorized users to add, delete, list, and edit products. The class was initially generated from a scaffold, but has since been modified, so do not regenerate.
Only logged in administratiosn can use the actions here. See Application.authorize for details.
See also: Product
Get information on a new product and attempt to create a row in the database.
# File app/controllers/admin_controller.rb, line 39
39: def create
40: @product = Product.new(@params[:product])
41: if @product.save
42: flash['notice'] = 'Product was successfully created.'
43: redirect_to :action => 'list'
44: else
45: render_action 'new'
46: end
47: end
Destroy a particular product.
# File app/controllers/admin_controller.rb, line 68
68: def destroy
69: Product.find(@params[:id]).destroy
70: redirect_to :action => 'list'
71: end
List all current products.
# File app/controllers/admin_controller.rb, line 22
22: def list
23: @product_pages, @products = paginate :product, :per_page => 10
24: end
Ship a number of products. This action normally dispatches back to itself. Each time it first looks for orders that the user has marked to be shipped and ships them. It then displays an updated list of orders still awaiting shipping.
The view contains a checkbox for each pending order. If the user selects the checkbox to ship the product with id 123, then this method will see things_to_ship[123] set to "yes".
# File app/controllers/admin_controller.rb, line 81
81: def ship
82: count = 0
83: if things_to_ship = params[:to_be_shipped]
84: count = do_shipping(things_to_ship)
85: end
86: count_text = pluralize(count, "order")
87: flash[:notice] = "#{count_text} marked as shipped"
88: @pending_orders = Order.pending_shipping
89: end
Show details of a particular product.
# File app/controllers/admin_controller.rb, line 27
27: def show
28: @product = Product.find(@params[:id])
29: end
Update an existing product based on values from the form.
# File app/controllers/admin_controller.rb, line 57
57: def update
58: @product = Product.find(@params[:id])
59: if @product.update_attributes(@params[:product])
60: flash['notice'] = 'Product was successfully updated.'
61: redirect_to :action => 'show', :id => @product
62: else
63: render_action 'edit'
64: end
65: end