Class: Bootsy::ImagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/bootsy/images_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /images POST /images.json



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/bootsy/images_controller.rb', line 33

def create
  @gallery = find_gallery
  @gallery.save! unless @gallery.persisted?
  @image = Image.new image_params
  @image.image_gallery_id = @gallery.id

  respond_to do |format|
    if @image.save
      image_view = render_to_string(file: 'bootsy/images/_image',
                                    formats: [:html],
                                    locals: { image: @image })

      new_image = render_to_string(file: 'bootsy/images/_new',
                                    formats: [:html],
                                    locals: { gallery: @gallery, image: @gallery.images.new })

      format.json { render json: { image: image_view, form: new_image, gallery_id: @gallery.id } }
    else
      format.json { render json: @image.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /images/1 DELETE /images/1.json



58
59
60
61
62
63
64
65
66
# File 'app/controllers/bootsy/images_controller.rb', line 58

def destroy
  @image = Image.find(params[:id])
  @image.destroy

  respond_to do |format|
    format.json { render json: { id: params[:id] } }
    format.html { redirect_to images_url }
  end
end

#indexObject

GET /images GET /images.json



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/bootsy/images_controller.rb', line 7

def index
  @gallery = find_gallery
  @images = @gallery.images

  respond_to do |format|
    format.html # index.html.erb
    format.json do
      rendered_images = []

      @images.each do |image|
        rendered_images << render_to_string(file: 'bootsy/images/_image',
                                      formats: [:html],
                                      locals: { image: image })
      end

      new_image = render_to_string(file: 'bootsy/images/_new',
                                    formats: [:html],
                                    locals: { gallery: @gallery, image: @gallery.images.new })

      render json: { images: rendered_images, form: new_image }
    end
  end
end