Skip to content

Instantly share code, notes, and snippets.

View MaryKuz's full-sized avatar

Maria Kuz MaryKuz

View GitHub Profile
@MaryKuz
MaryKuz / create Singleton Resource api_session
Created March 28, 2019 12:05
Create the singleton resource api/session
Rails.application.routes.draw do
namespace :api do
...
resource :session, only: [:create, :destroy]
end
end
@MaryKuz
MaryKuz / Adding Test to User Model
Created March 28, 2019 12:03
Adding the test to the User model
it { should have_one(:auth_token).dependent(:destroy) }
@MaryKuz
MaryKuz / Write Tests for AuthToken Model
Created March 28, 2019 12:02
Write tests for the AuthToken model
require 'rails_helper'
RSpec.describe AuthToken, type: :model do
it { should belong_to :user }
it { should validate_presence_of :value }
end
@MaryKuz
MaryKuz / Add Code to User Model
Created March 28, 2019 12:01
Add code to User model
class User < ActiveRecord::Base
...
has_one :auth_token, dependent: :destroy
...
end
@MaryKuz
MaryKuz / Add Code to Model AuthToken
Created March 28, 2019 12:00
Add code to model AuthToken
class AuthToken < ActiveRecord::Base
belongs_to :user
validates :value, presence: true
end
@MaryKuz
MaryKuz / Create the AuthToken
Created March 28, 2019 11:58
Create the AuthToken
class CreateAuthTokens < ActiveRecord::Migration
def change
create_table :auth_tokens do |t|
t.string :value
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
@MaryKuz
MaryKuz / Registration Request Error
Created March 28, 2019 11:54
Registration request error
{
"errors": {
"email": [
"has already been taken"
]
}
}
@MaryKuz
MaryKuz / Registration Test
Created March 28, 2019 11:53
Registration test
{
"email": "test@test.com",
"name": "test"
}
@MaryKuz
MaryKuz / Add Code to ApplicationController
Created March 28, 2019 11:50
Add code to ApplicationController
class ApplicationController < ActionController::Base
...
skip_before_action :verify_authenticity_token, if: :json_request?
private
def json_request?
request.format.json?
end
...
@MaryKuz
MaryKuz / Catch Error in ApplicationController
Created March 28, 2019 11:46
Catch error in ApplicationController
class ApplicationController < ActionController::Base
...
rescue_from ActiveRecord::RecordInvalid do
render :errors, status: :unprocessable_entity
end
...
end