Module: ActionCrud::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/action_crud/controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /model



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/action_crud/controller.rb', line 100

def create
  self.record = model.new(record_params)

  respond_to do |format|
    if record.save
      format.html { redirect_to edit_record_path(record), notice: "#{model} was successfully created." }
      format.json { render json: record, status: :created }
    else
      format.html { render :new }
      format.json { render json: record.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /model/1



128
129
130
131
132
133
134
135
# File 'lib/action_crud/controller.rb', line 128

def destroy
  record.destroy

  respond_to do |format|
    format.html { redirect_to records_path, notice: "#{model} was successfully destroyed." }
    format.json { head :no_content }
  end
end

#editObject

GET /model/1/edit



92
93
94
95
96
97
# File 'lib/action_crud/controller.rb', line 92

def edit
  respond_to do |format|
    format.html { render :edit }
    format.json { render json: record }
  end
end

#indexObject

GET /model



63
64
65
66
67
68
69
70
71
# File 'lib/action_crud/controller.rb', line 63

def index
  self.records = model.send index_scope
  self.records = paginate(records) if respond_to? :per_page

  respond_to do |format|
    format.html { render :index }
    format.json { render json: records }
  end
end

#modelObject Also known as: current_model

Get model



138
139
140
# File 'lib/action_crud/controller.rb', line 138

def model
  model_class
end

#newObject

GET /model/new



74
75
76
77
78
79
80
81
# File 'lib/action_crud/controller.rb', line 74

def new
  self.record = model.new

  respond_to do |format|
    format.html { render :new }
    format.json { render json: record }
  end
end

#recordObject Also known as: current_record

Get single record



145
146
147
# File 'lib/action_crud/controller.rb', line 145

def record
  instance_variable_get "@#{instance_name}"
end

#recordsObject Also known as: current_records

Get records collection



152
153
154
# File 'lib/action_crud/controller.rb', line 152

def records
  instance_variable_get "@#{collection_name}"
end

#showObject

GET /model/1



84
85
86
87
88
89
# File 'lib/action_crud/controller.rb', line 84

def show
  respond_to do |format|
    format.html { render :show }
    format.json { render json: record }
  end
end

#updateObject

PATCH/PUT /model/1



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/action_crud/controller.rb', line 115

def update
  respond_to do |format|
    if record.update(record_params)
      format.html { redirect_to edit_record_path(record), notice: "#{model} was successfully updated." }
      format.json { render json: record, status: :ok }
    else
      format.html { render :edit }
      format.json { render json: record.errors, status: :unprocessable_entity }
    end
  end
end