Add "rentable type" to rentables
[cs356-p2-videostore.git] / app / controllers / video_controller.rb
1 class VideoController < ApplicationController
2   def index
3     list
4     render :action => 'list'
5   end
6
7   # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
8   verify :method => :post, :only => [ :destroy, :create, :update ],
9          :redirect_to => { :action => :list }
10
11   def list
12     @video_pages, @videos = paginate :videos, :per_page => 10
13   end
14
15   def show
16     @video = Video.find(params[:id])
17   end
18
19   def new
20     @video = Video.new
21   end
22
23   def create
24     # A new rentable must be created and saved whenever we create a new
25     # video. This is so we have a rentable_id to add to the video.
26     @rentable = Rentable.new
27     @rentable.rtype = 'video'
28     @rentable.save!
29     @video = Video.new(params[:video])
30     @video.rentable_id = @rentable.id
31     if @video.save
32       flash[:notice] = 'Video was successfully created.'
33       redirect_to :action => 'list'
34     else
35       render :action => 'new'
36     end
37   end
38
39   def edit
40     @video = Video.find(params[:id])
41   end
42
43   def update
44     @video = Video.find(params[:id])
45     if @video.update_attributes(params[:video])
46       flash[:notice] = 'Video was successfully updated.'
47       redirect_to :action => 'show', :id => @video
48     else
49       render :action => 'edit'
50     end
51   end
52
53   def destroy
54     Video.find(params[:id]).destroy
55     redirect_to :action => 'list'
56   end
57 end