Fix object relationships between video and rentable
[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.save!
28     @video = Video.new(params[:video])
29     @video.rentable_id = @rentable.id
30     if @video.save
31       flash[:notice] = 'Video was successfully created.'
32       redirect_to :action => 'list'
33     else
34       render :action => 'new'
35     end
36   end
37
38   def edit
39     @video = Video.find(params[:id])
40   end
41
42   def update
43     @video = Video.find(params[:id])
44     if @video.update_attributes(params[:video])
45       flash[:notice] = 'Video was successfully updated.'
46       redirect_to :action => 'show', :id => @video
47     else
48       render :action => 'edit'
49     end
50   end
51
52   def destroy
53     Video.find(params[:id]).destroy
54     redirect_to :action => 'list'
55   end
56 end