Skip to content

Instantly share code, notes, and snippets.

def index
@posts = current_user.posts
end
# app/views/posts/index.jbuilder
json.array! @posts, :id, :title, :description, :created_at, ...
class PostsController < ApiController
def update
@post = current_user.posts.find_by(id: params[:id])
# For error message
if !@post
return render json: { error: 'Unable to find blog post' }, status: :not_found
end
if [email protected]_attributes(param[:post])
return render json: {
error: "Could not update post",
class ApiController < ApplicationController::Base
rescue_from ActiveRecord::RecordNotFound do |e|
render json: { error: e.message }, status: :not_found
end
rescue_from ActiveRecord::RecordInvalid do |invalid|
# You can even use jbuilder templates to make this cleaner!
render 'shared/record_invalid', locals: { exception: invalid },
status: :unprocessable_entity
class PostsController < ApiController
def update
@post = current_user.posts.find(params[:id])
@post.update_attributes!(params[:post])
end
def create
@post = current_user.posts.create!(params[:post])
end
end
class ApiController < ActionController::Base
rescue_from ActionController::ParameterMissing do |e|
# You can even render a jbuilder template too!
render json: {error: e.message }, status: :unprocessable_entity
end
end
class PostsController < ApiController
def create
@post = current_user.posts.create!(params.require(:post))
end
end
@cthornton
cthornton / update_ghost.rb
Created September 10, 2014 06:04
Automatically Update Ghost Blog
#!/usr/bin/env ruby
#
# Automatically updates ghost per instructions at:
#
# http://support.ghost.org/how-to-upgrade/
#
# Make sure to back up your files before proceeding (i.e. git repo)!
#
require 'tempfile'
@cthornton
cthornton / pkcs7_degenerate.rb
Last active September 12, 2017 21:53
Ruby PKCS#7 Certificate Only Degenerate (application/x-x509-ca-ra-cert)
# Creates a degenerate PKCS#7 certificate only in ruby (SCEP application/x-x509-ca-ra-cert)
# Inspiration: https://github.com/AppBlade/TestHub/blob/master/app/controllers/scep_controller.rb#L92-L112
#
# Tested Ruby 2.2.0 OSX 10.10, OpenSSL 1.0.1l
require 'openssl'
cert = OpenSSL::X509::Certificate.new File.read('some_cert.crt')
# Fails ruby 2.2, OpenSSL 1.0.1l!!
p7certs = OpenSSL::PKCS7.new
@cthornton
cthornton / hashing.java
Created October 25, 2018 22:27
Hashing
// Example:
//
// System.out.println(Hasher.sha1("hello world")); // prints: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
// System.out.println(Hasher.sha1AsInt("hello world")); // prints: 896314922
import java.nio.charset.*;
import com.google.common.hash.*;
class Hasher {
public static String sha1(String input) {