ruby
38 lines · 2 tabs
Maya Patel
Jan 2026
2 tabs
module MyApp
class Application < Rails::Application
config.load_defaults 6.1
# API-only mode
config.api_only = true
# CORS configuration
config.middleware.insert_before 0, Rack::Cors do
allow do
origins_list = if Rails.env.production?
ENV['FRONTEND_URL']
else
'localhost:5173', 'localhost:3001', /127\.0\.0\.1:\d+/
end
origins origins_list
resource '/api/*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head],
credentials: true,
expose: ['Authorization']
end
end
end
end
class ApplicationController < ActionController::API
include ActionController::Cookies
before_action :set_default_format
private
def set_default_format
request.format = :json
end
end
2 files · ruby
Explain with highlit
When building a React SPA, I configure Rails in API-only mode to skip view rendering, asset pipeline, and session cookies. The --api flag generates a lean Rails app focused on JSON responses. I enable CORS to allow the React dev server on localhost:5173 to communicate with Rails during development, while restricting origins in production. The rack-cors gem handles preflight OPTIONS requests automatically. I also configure Rails to use token-based authentication instead of session cookies since SPAs can't rely on same-origin cookie behavior. This setup provides a clean separation between backend API concerns and frontend presentation logic, making both easier to scale and deploy independently.
Related snips
ruby
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
rails
hotwire
turbo
by codesnips
4 tabs
ruby
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
ActiveRecord scopes for reusable query logic
rails
activerecord
patterns
by Alex Kumar
1 tab
ruby
payload = {
sub: user.id,
iss: 'https://auth.example.com',
aud: 'codesnips-api',
exp: 15.minutes.from_now.to_i,
iat: Time.now.to_i,
JWT issuance and verification without common footguns
jwt
authentication
api
by Kai Nakamura
2 tabs
ruby
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
rails
caching
http-caching
by Alex Kumar
1 tab
ruby
class PostsController < ApplicationController
def index
@posts = Post.includes(:author)
.order(created_at: :desc)
.page(params[:page])
.per(10)
Turbo Frames: infinite scroll with lazy-loading frame
rails
turbo
hotwire
by codesnips
4 tabs
ruby
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
rails
performance
streaming
by codesnips
3 tabs
Share this code
Here's the card — post it anywhere.