У меня есть модель со следующими параметрами.
create_table "colegios", force: :cascade do |t|
t.float "Kids"
t.float "Girls"
t.float "total_persons"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Я хочу использовать метод new
, где я ввожу число kids
и girls
и хочу обновить параметр total_persons
, который будет суммой kids
и girls
.
Мой контроллер:
def new
@colegio = Colegios.new
end
# GET /colegios/1/edit
def edit
end
# POST /colegios
# POST /colegios.json
def create
@colegio = Colegios.new(colegio_params)
respond_to do |format|
if @colegio.save
format.html { redirect_to @colegio, notice: 'Successfully created.' }
format.json { render :show, status: :created, location: @colegio }
else
format.html { render :new }
format.json { render json: @colegio.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /colegios/1
# PATCH/PUT /colegios/1.json
def update
respond_to do |format|
if @colegio.update(colegio_params)
format.html { redirect_to @colegio, notice: 'Successfully updated.' }
format.json { render :show, status: :ok, location: @colegio }
else
format.html { render :edit }
format.json { render json: @colegio.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_colegio
@colegio = Colegio.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def colegio_params
params.require(:colegio).permit(:niños, :niñas, :total_personas)
end