scaffold accounts

This commit is contained in:
2016-08-27 21:40:21 -05:00
parent 8ff9ca02f8
commit 3d5743b92f
16 changed files with 289 additions and 1 deletions

View File

@ -0,0 +1,75 @@
class AccountsController < ApplicationController
before_action :set_account, only: [:show, :edit, :update, :destroy]
# GET /accounts
# GET /accounts.json
def index
@accounts = Account.all
end
# GET /accounts/1
# GET /accounts/1.json
def show
end
# GET /accounts/new
def new
@account = Account.new
end
# GET /accounts/1/edit
def edit
end
# POST /accounts
# POST /accounts.json
def create
@account = Account.new(account_params)
respond_to do |format|
if @account.save
format.html { redirect_to @account, notice: 'Account was successfully created.' }
format.json { render :show, status: :created, location: @account }
else
format.html { render :new }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /accounts/1
# PATCH/PUT /accounts/1.json
def update
respond_to do |format|
if @account.update(account_params)
format.html { redirect_to @account, notice: 'Account was successfully updated.' }
format.json { render :show, status: :ok, location: @account }
else
format.html { render :edit }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
# DELETE /accounts/1
# DELETE /accounts/1.json
def destroy
@account.destroy
respond_to do |format|
format.html { redirect_to accounts_url, notice: 'Account was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_account
@account = Account.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def account_params
params.require(:account).permit(:username, :password, :home, :site)
end
end

2
app/models/account.rb Normal file
View File

@ -0,0 +1,2 @@
class Account < ApplicationRecord
end

View File

@ -0,0 +1,2 @@
json.extract! account, :id, :username, :password, :home, :site, :created_at, :updated_at
json.url account_url(account, format: :json)

View File

@ -0,0 +1,37 @@
<%= form_for(account) do |f| %>
<% if account.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(account.errors.count, "error") %> prohibited this account from being saved:</h2>
<ul>
<% account.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>
</div>
<div class="field">
<%= f.label :home %>
<%= f.text_field :home %>
</div>
<div class="field">
<%= f.label :site %>
<%= f.text_field :site %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

@ -0,0 +1,6 @@
<h1>Editing Account</h1>
<%= render 'form', account: @account %>
<%= link_to 'Show', @account %> |
<%= link_to 'Back', accounts_path %>

View File

@ -0,0 +1,33 @@
<p id="notice"><%= notice %></p>
<h1>Accounts</h1>
<table>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Home</th>
<th>Site</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @accounts.each do |account| %>
<tr>
<td><%= account.username %></td>
<td><%= account.password %></td>
<td><%= account.home %></td>
<td><%= account.site %></td>
<td><%= link_to 'Show', account %></td>
<td><%= link_to 'Edit', edit_account_path(account) %></td>
<td><%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Account', new_account_path %>

View File

@ -0,0 +1 @@
json.array! @accounts, partial: 'accounts/account', as: :account

View File

@ -0,0 +1,5 @@
<h1>New Account</h1>
<%= render 'form', account: @account %>
<%= link_to 'Back', accounts_path %>

View File

@ -0,0 +1,24 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Username:</strong>
<%= @account.username %>
</p>
<p>
<strong>Password:</strong>
<%= @account.password %>
</p>
<p>
<strong>Home:</strong>
<%= @account.home %>
</p>
<p>
<strong>Site:</strong>
<%= @account.site %>
</p>
<%= link_to 'Edit', edit_account_path(@account) %> |
<%= link_to 'Back', accounts_path %>

View File

@ -0,0 +1 @@
json.partial! "accounts/account", account: @account