mirror of
https://github.com/edcommonwealth/Dashboard.git
synced 2026-03-15 09:45:53 -07:00
set up example scaffold with rspec + factorybot tests
This commit is contained in:
parent
533c921a6d
commit
d1e6fb1e39
23 changed files with 512 additions and 6 deletions
60
app/controllers/dashboard/examples_controller.rb
Normal file
60
app/controllers/dashboard/examples_controller.rb
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
module Dashboard
|
||||
class ExamplesController < ApplicationController
|
||||
before_action :set_example, only: %i[ show edit update destroy ]
|
||||
|
||||
# GET /examples
|
||||
def index
|
||||
@examples = Example.all
|
||||
end
|
||||
|
||||
# GET /examples/1
|
||||
def show
|
||||
end
|
||||
|
||||
# GET /examples/new
|
||||
def new
|
||||
@example = Example.new
|
||||
end
|
||||
|
||||
# GET /examples/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /examples
|
||||
def create
|
||||
@example = Example.new(example_params)
|
||||
|
||||
if @example.save
|
||||
redirect_to @example, notice: "Example was successfully created."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /examples/1
|
||||
def update
|
||||
if @example.update(example_params)
|
||||
redirect_to @example, notice: "Example was successfully updated.", status: :see_other
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /examples/1
|
||||
def destroy
|
||||
@example.destroy!
|
||||
redirect_to examples_url, notice: "Example was successfully destroyed.", status: :see_other
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_example
|
||||
@example = Example.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def example_params
|
||||
params.require(:example).permit(:text, :body)
|
||||
end
|
||||
end
|
||||
end
|
||||
4
app/helpers/dashboard/examples_helper.rb
Normal file
4
app/helpers/dashboard/examples_helper.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
module Dashboard
|
||||
module ExamplesHelper
|
||||
end
|
||||
end
|
||||
4
app/models/dashboard/example.rb
Normal file
4
app/models/dashboard/example.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
module Dashboard
|
||||
class Example < ApplicationRecord
|
||||
end
|
||||
end
|
||||
12
app/views/dashboard/examples/_example.html.erb
Normal file
12
app/views/dashboard/examples/_example.html.erb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<div id="<%= dom_id example %>">
|
||||
<p>
|
||||
<strong>Text:</strong>
|
||||
<%= example.text %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Body:</strong>
|
||||
<%= example.body %>
|
||||
</p>
|
||||
|
||||
</div>
|
||||
27
app/views/dashboard/examples/_form.html.erb
Normal file
27
app/views/dashboard/examples/_form.html.erb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<%= form_with(model: example) do |form| %>
|
||||
<% if example.errors.any? %>
|
||||
<div style="color: red">
|
||||
<h2><%= pluralize(example.errors.count, "error") %> prohibited this example from being saved:</h2>
|
||||
|
||||
<ul>
|
||||
<% example.errors.each do |error| %>
|
||||
<li><%= error.full_message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= form.label :text, style: "display: block" %>
|
||||
<%= form.text_field :text %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :body, style: "display: block" %>
|
||||
<%= form.text_area :body %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.submit %>
|
||||
</div>
|
||||
<% end %>
|
||||
10
app/views/dashboard/examples/edit.html.erb
Normal file
10
app/views/dashboard/examples/edit.html.erb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<h1>Editing example</h1>
|
||||
|
||||
<%= render "form", example: @example %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Show this example", @example %> |
|
||||
<%= link_to "Back to examples", examples_path %>
|
||||
</div>
|
||||
14
app/views/dashboard/examples/index.html.erb
Normal file
14
app/views/dashboard/examples/index.html.erb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<p style="color: green"><%= notice %></p>
|
||||
|
||||
<h1>Examples</h1>
|
||||
|
||||
<div id="examples">
|
||||
<% @examples.each do |example| %>
|
||||
<%= render example %>
|
||||
<p>
|
||||
<%= link_to "Show this example", example %>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= link_to "New example", new_example_path %>
|
||||
9
app/views/dashboard/examples/new.html.erb
Normal file
9
app/views/dashboard/examples/new.html.erb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<h1>New example</h1>
|
||||
|
||||
<%= render "form", example: @example %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Back to examples", examples_path %>
|
||||
</div>
|
||||
10
app/views/dashboard/examples/show.html.erb
Normal file
10
app/views/dashboard/examples/show.html.erb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<p style="color: green"><%= notice %></p>
|
||||
|
||||
<%= render @example %>
|
||||
|
||||
<div>
|
||||
<%= link_to "Edit this example", edit_example_path(@example) %> |
|
||||
<%= link_to "Back to examples", examples_path %>
|
||||
|
||||
<%= button_to "Destroy this example", @example, method: :delete %>
|
||||
</div>
|
||||
Loading…
Add table
Add a link
Reference in a new issue