Class: SnippetsController

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

Constant Summary

Constants inherited from ApplicationController

ApplicationController::ALLOWABLE_CONFIGS

Instance Method Summary collapse

Instance Method Details

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/snippets_controller.rb', line 7

def create
  respond_to do |format|
    format.json do
      snippet = Snippet.create(snippet_params.merge(user_id: current_user.id))

      if snippet.errors.any?
        render json: snippet.errors.full_messages, status: :unprocessable_entity
      else
        render json: snippet, status: :created
      end
    end
  end
end

#destroyObject



49
50
51
52
53
54
55
56
# File 'app/controllers/snippets_controller.rb', line 49

def destroy
  respond_to do |format|
    format.json do
      @snippet.destroy!
      render json: @snippet
    end
  end
end

#indexObject



42
43
44
45
46
47
# File 'app/controllers/snippets_controller.rb', line 42

def index
  respond_to do |format|
    format.html
    format.json { render json: Snippet.all }
  end
end

#showObject



35
36
37
38
39
40
# File 'app/controllers/snippets_controller.rb', line 35

def show
  respond_to do |format|
    format.json { render json: @snippet }
    format.html { render template: 'application/index' }
  end
end

#updateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/snippets_controller.rb', line 21

def update
  respond_to do |format|
    format.json do
      @snippet.update_attributes!(snippet_params)

      if @snippet.errors.any?
        render json: @snippet.errors.full_messages, status: :unprocessable_entity
      else
        render json: @snippet
      end
    end
  end
end