ruby
34 lines · 7 steps
The new/create pattern in a Rails controller
How a Rails controller scopes records to the current account and handles the save-or-re-render flow.
Explained by
highlit
1class ProjectsController < ApplicationController
2 def new
3 @project = current_account.projects.build
4 load_form_collections
5 end
6
7 def create
8 @project = current_account.projects.build(project_params)
9
10 if @project.save
11 redirect_to @project, notice: "Project created."
12 else
13 load_form_collections
14 render :new, status: :unprocessable_entity
15 end
16 end
17
18 private
19
20 def load_form_collections
21 @clients = current_account.clients
22 .includes(:contacts)
23 .active
24 .order(:name)
25
26 @team_members = current_account.memberships
27 .includes(:user)
28 .order("users.last_name")
29 end
30
31 def project_params
32 params.require(:project).permit(:name, :client_id, :lead_id, :due_on)
33 end
34end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Building records through an association like current_account.projects keeps every query and creation scoped to the right tenant automatically.
- 2Re-rendering :new with an unprocessable_entity status on failure preserves user input and validation errors while signaling the error correctly.
- 3Extracting shared setup like form collections into a private method keeps new and create in sync without duplication.
Related explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/the-new-create-pattern-in-a-rails-controller-explained-ruby-9571/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.