Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AlbumsController < ApplicationController
- # login is required for the following actions
- before_filter :login_required, :only => [ :add, :delete, :edit ]
- def add
- if request.post?
- @album = Album.new()
- @album.title = params[:album][:title]
- @album.user = @current_user
- @album.save
- # assing each selected photo to this new album
- if not params[:image].nil?
- params[:image].each{ |i|
- # i contains the image id and 1 if selected or 0 if not selected i=[image_id, selected?]
- if i[1]=="1" # image was selected
- p=Photo.find_by_id(i[0])
- p.album = @album
- p.save
- end
- }
- end
- redirect_to( :action => "show", :id => @album.id)
- end
- @unassigned_photos = Photo.find_all_by_album_id(nil)
- end
- def show
- if params[:id]=="" or params[:id].nil? # no album selected
- redirect_to(:action => "all", :id => "")
- else
- @album = Album.find_by_id(params[:id])
- @photos = @album.photos
- end
- end
- def all
- @albums = Album.find(:all)
- end
- def delete
- @album = Album.find_by_id(params[:id])
- @album.photos.each{ |p|
- p.album_id = nil
- p.save
- }
- @album.destroy
- redirect_to(:action => "all", :id => "")
- end
- def edit
- if request.post?
- @album = Album.find_by_id(params[:id])
- @unassigned_photos = Photo.find_all_by_album_id(nil)
- if not params[:album].nil?
- @album.title = params[:album]["title"]
- @album.user = @current_user
- # assing each selected photo to this new album
- if not params[:image].nil?
- params[:image].each{ |i|
- # i contains the image id and 1 if selected or 0 if not selected i=[image_id, selected?]
- if i[1]=="1" # image was selected
- p=Photo.find_by_id(i[0])
- p.album = @album
- p.save
- else
- p=Photo.find_by_id(i[0])
- p.album = nil
- p.save
- end
- }
- end
- @album.save
- redirect_to(:action => "show", :id => @album.id)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment