Skip to content

Instantly share code, notes, and snippets.

@JamesZoft
Last active January 25, 2019 14:02
Show Gist options
  • Save JamesZoft/62e4beece5dd4e3e3473634ddc25b601 to your computer and use it in GitHub Desktop.
Save JamesZoft/62e4beece5dd4e3e3473634ddc25b601 to your computer and use it in GitHub Desktop.
class Reminder < ApplicationRecord
attr_accessor :reminder_text, :reminder_date
end
# frozen_string_literal: true
# API controller for Reminder
class ReminderController < ApplicationController
# GET /reminders
def index
@reminders = Reminder.all
json_response(@reminders)
end
# POST /reminder
def create
@reminder = Reminder.create!(reminder_params)
json_response(@reminder, :created)
end
# GET /reminder/:id
def show
json_response(@reminder)
end
# DELETE /reminder/:id
def destroy
@reminder.destroy
head :no_content
end
private
def reminder_params
# whitelist params
pars = params.permit(:reminder_text, :reminder_date)
puts pars
pars
end
def set_reminder
@reminder = Reminder.find(params[:id])
end
end
# frozen_string_literal: true
# Return a 200 response with the given object
module Response
def json_response(object, status = :ok)
puts object
render json: object, status: status
end
end
Started POST "/reminder" for 127.0.0.1 at 2019-01-25 13:58:31 +0000
(0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /Library/Ruby/Gems/2.3.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by ReminderController#create as */*
Parameters: {"reminder_text"=>"hi", "reminder_date"=>1548350451, "reminder"=>{"reminder_text"=>"hi", "reminder_date"=>1548350451}}
Unpermitted parameter: :reminder
putting pars
{"reminder_text"=>"hi", "reminder_date"=>1548350451}
(0.0ms) begin transaction
↳ app/controllers/reminder_controller.rb:13
Reminder Create (0.4ms) INSERT INTO "reminders" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2019-01-25 13:58:31.343583"], ["updated_at", "2019-01-25 13:58:31.343583"]]
↳ app/controllers/reminder_controller.rb:13
(0.8ms) commit transaction
↳ app/controllers/reminder_controller.rb:13
#<Reminder:0x007fc7209e1850>
Completed 201 Created in 30ms (Views: 0.7ms | ActiveRecord: 2.6ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment