Module: ActionCrud::Controller

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /model



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

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



140
141
142
143
144
145
146
147
# File 'lib/action_crud/controller.rb', line 140

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



104
105
106
107
108
109
# File 'lib/action_crud/controller.rb', line 104

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

#indexObject

GET /model



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

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



150
151
152
# File 'lib/action_crud/controller.rb', line 150

def model
  record.nil? ? model_class : record.class
end

#newObject

GET /model/new



86
87
88
89
90
91
92
93
# File 'lib/action_crud/controller.rb', line 86

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



157
158
159
# File 'lib/action_crud/controller.rb', line 157

def record
  instance_variable_get "@#{instance_name}"
end

#recordsObject Also known as: current_records

Get records collection



164
165
166
# File 'lib/action_crud/controller.rb', line 164

def records
  instance_variable_get "@#{collection_name}"
end

#showObject

GET /model/1



96
97
98
99
100
101
# File 'lib/action_crud/controller.rb', line 96

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

#updateObject

PATCH/PUT /model/1



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

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