Guest User

Untitled

a guest
May 24th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. class AlbumsController < ApplicationController
  2.  
  3. # login is required for the following actions
  4. before_filter :login_required, :only => [ :add, :delete, :edit ]
  5.  
  6. def add
  7. if request.post?
  8. @album = Album.new()
  9. @album.title = params[:album][:title]
  10. @album.user = @current_user
  11. @album.save
  12.  
  13. # assing each selected photo to this new album
  14. if not params[:image].nil?
  15. params[:image].each{ |i|
  16. # i contains the image id and 1 if selected or 0 if not selected i=[image_id, selected?]
  17. if i[1]=="1" # image was selected
  18. p=Photo.find_by_id(i[0])
  19. p.album = @album
  20. p.save
  21. end
  22. }
  23. end
  24. redirect_to( :action => "show", :id => @album.id)
  25. end
  26. @unassigned_photos = Photo.find_all_by_album_id(nil)
  27. end
  28.  
  29. def show
  30. if params[:id]=="" or params[:id].nil? # no album selected
  31. redirect_to(:action => "all", :id => "")
  32. else
  33. @album = Album.find_by_id(params[:id])
  34. @photos = @album.photos
  35. end
  36. end
  37.  
  38. def all
  39. @albums = Album.find(:all)
  40. end
  41.  
  42. def delete
  43. @album = Album.find_by_id(params[:id])
  44. @album.photos.each{ |p|
  45. p.album_id = nil
  46. p.save
  47. }
  48. @album.destroy
  49. redirect_to(:action => "all", :id => "")
  50. end
  51.  
  52. def edit
  53. if request.post?
  54. @album = Album.find_by_id(params[:id])
  55. @unassigned_photos = Photo.find_all_by_album_id(nil)
  56. if not params[:album].nil?
  57. @album.title = params[:album]["title"]
  58. @album.user = @current_user
  59. # assing each selected photo to this new album
  60. if not params[:image].nil?
  61. params[:image].each{ |i|
  62. # i contains the image id and 1 if selected or 0 if not selected i=[image_id, selected?]
  63. if i[1]=="1" # image was selected
  64. p=Photo.find_by_id(i[0])
  65. p.album = @album
  66. p.save
  67. else
  68. p=Photo.find_by_id(i[0])
  69. p.album = nil
  70. p.save
  71. end
  72. }
  73. end
  74. @album.save
  75. redirect_to(:action => "show", :id => @album.id)
  76. end
  77. end
  78. end
  79.  
  80. end
Add Comment
Please, Sign In to add comment