Skip to content

Commit e948b11

Browse files
committed
Generate scaffold for Snippets
1 parent 9128abc commit e948b11

20 files changed

+387
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class SnippetsController < ApplicationController
2+
before_action :set_snippet, only: %i[show edit update destroy]
3+
4+
# GET /snippets
5+
def index
6+
@snippets = Snippet.all
7+
end
8+
9+
# GET /snippets/1
10+
def show
11+
end
12+
13+
# GET /snippets/new
14+
def new
15+
@snippet = Snippet.new
16+
end
17+
18+
# GET /snippets/1/edit
19+
def edit
20+
end
21+
22+
# POST /snippets
23+
def create
24+
@snippet = Snippet.new(snippet_params)
25+
26+
if @snippet.save
27+
redirect_to @snippet, notice: "Snippet was successfully created."
28+
else
29+
render :new, status: :unprocessable_entity
30+
end
31+
end
32+
33+
# PATCH/PUT /snippets/1
34+
def update
35+
if @snippet.update(snippet_params)
36+
redirect_to @snippet, notice: "Snippet was successfully updated.", status: :see_other
37+
else
38+
render :edit, status: :unprocessable_entity
39+
end
40+
end
41+
42+
# DELETE /snippets/1
43+
def destroy
44+
@snippet.destroy!
45+
redirect_to snippets_url, notice: "Snippet was successfully destroyed.", status: :see_other
46+
end
47+
48+
private
49+
50+
# Use callbacks to share common setup or constraints between actions.
51+
def set_snippet
52+
@snippet = Snippet.find(params[:id])
53+
end
54+
55+
# Only allow a list of trusted parameters through.
56+
def snippet_params
57+
params.fetch(:snippet, {})
58+
end
59+
end

app/helpers/snippets_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module SnippetsHelper
2+
end

app/models/snippet.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Snippet < ApplicationRecord
2+
end

app/views/snippets/_form.html.erb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<%= form_with(model: snippet) do |form| %>
2+
<% if snippet.errors.any? %>
3+
<div style="color: red">
4+
<h2><%= pluralize(snippet.errors.count, "error") %> prohibited this snippet from being saved:</h2>
5+
6+
<ul>
7+
<% snippet.errors.each do |error| %>
8+
<li><%= error.full_message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div>
15+
<%= form.submit %>
16+
</div>
17+
<% end %>

app/views/snippets/_snippet.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div id="<%= dom_id snippet %>">
2+
</div>

app/views/snippets/edit.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h1>Editing snippet</h1>
2+
3+
<%= render "form", snippet: @snippet %>
4+
5+
<br>
6+
7+
<div>
8+
<%= link_to "Show this snippet", @snippet %> |
9+
<%= link_to "Back to snippets", snippets_path %>
10+
</div>

app/views/snippets/index.html.erb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p style="color: green"><%= notice %></p>
2+
3+
<h1>Snippets</h1>
4+
5+
<div id="snippets">
6+
<% @snippets.each do |snippet| %>
7+
<%= render snippet %>
8+
<p>
9+
<%= link_to "Show this snippet", snippet %>
10+
</p>
11+
<% end %>
12+
</div>
13+
14+
<%= link_to "New snippet", new_snippet_path %>

app/views/snippets/new.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>New snippet</h1>
2+
3+
<%= render "form", snippet: @snippet %>
4+
5+
<br>
6+
7+
<div>
8+
<%= link_to "Back to snippets", snippets_path %>
9+
</div>

app/views/snippets/show.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<p style="color: green"><%= notice %></p>
2+
3+
<%= render @snippet %>
4+
5+
<div>
6+
<%= link_to "Edit this snippet", edit_snippet_path(@snippet) %> |
7+
<%= link_to "Back to snippets", snippets_path %>
8+
9+
<%= button_to "Destroy this snippet", @snippet, method: :delete %>
10+
</div>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
55
Rails.application.routes.draw do
6+
resources :snippets
67
# Redirects www to root domain
78
match "(*any)", to: redirect(subdomain: ""), via: :all, constraints: {subdomain: "www"}
89

0 commit comments

Comments
 (0)