created crud scaffold
This commit is contained in:
parent
1cd5e05da0
commit
2140c15eca
|
@ -0,0 +1,3 @@
|
|||
// Place all the styles related to the friends controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: https://sass-lang.com/
|
|
@ -0,0 +1,74 @@
|
|||
class FriendsController < ApplicationController
|
||||
before_action :set_friend, only: [:show, :edit, :update, :destroy]
|
||||
|
||||
# GET /friends
|
||||
# GET /friends.json
|
||||
def index
|
||||
@friends = Friend.all
|
||||
end
|
||||
|
||||
# GET /friends/1
|
||||
# GET /friends/1.json
|
||||
def show
|
||||
end
|
||||
|
||||
# GET /friends/new
|
||||
def new
|
||||
@friend = Friend.new
|
||||
end
|
||||
|
||||
# GET /friends/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /friends
|
||||
# POST /friends.json
|
||||
def create
|
||||
@friend = Friend.new(friend_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @friend.save
|
||||
format.html { redirect_to @friend, notice: 'Friend was successfully created.' }
|
||||
format.json { render :show, status: :created, location: @friend }
|
||||
else
|
||||
format.html { render :new }
|
||||
format.json { render json: @friend.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /friends/1
|
||||
# PATCH/PUT /friends/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @friend.update(friend_params)
|
||||
format.html { redirect_to @friend, notice: 'Friend was successfully updated.' }
|
||||
format.json { render :show, status: :ok, location: @friend }
|
||||
else
|
||||
format.html { render :edit }
|
||||
format.json { render json: @friend.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /friends/1
|
||||
# DELETE /friends/1.json
|
||||
def destroy
|
||||
@friend.destroy
|
||||
respond_to do |format|
|
||||
format.html { redirect_to friends_url, notice: 'Friend was successfully destroyed.' }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_friend
|
||||
@friend = Friend.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def friend_params
|
||||
params.require(:friend).permit(:first_name, :last_name, :email, :phone, :twitter)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
module FriendsHelper
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
class Friend < ApplicationRecord
|
||||
end
|
|
@ -0,0 +1,42 @@
|
|||
<%= form_with(model: friend) do |form| %>
|
||||
<% if friend.errors.any? %>
|
||||
<div id="error_explanation">
|
||||
<h2><%= pluralize(friend.errors.count, "error") %> prohibited this friend from being saved:</h2>
|
||||
|
||||
<ul>
|
||||
<% friend.errors.each do |error| %>
|
||||
<li><%= error.full_message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :first_name %>
|
||||
<%= form.text_field :first_name %>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :last_name %>
|
||||
<%= form.text_field :last_name %>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :email %>
|
||||
<%= form.text_field :email %>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :phone %>
|
||||
<%= form.text_field :phone %>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<%= form.label :twitter %>
|
||||
<%= form.text_field :twitter %>
|
||||
</div>
|
||||
|
||||
<div class="actions">
|
||||
<%= form.submit %>
|
||||
</div>
|
||||
<% end %>
|
|
@ -0,0 +1,2 @@
|
|||
json.extract! friend, :id, :first_name, :last_name, :email, :phone, :twitter, :created_at, :updated_at
|
||||
json.url friend_url(friend, format: :json)
|
|
@ -0,0 +1,6 @@
|
|||
<h1>Editing Friend</h1>
|
||||
|
||||
<%= render 'form', friend: @friend %>
|
||||
|
||||
<%= link_to 'Show', @friend %> |
|
||||
<%= link_to 'Back', friends_path %>
|
|
@ -0,0 +1,35 @@
|
|||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<h1>Friend List</h1>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>First name</th>
|
||||
<th>Last name</th>
|
||||
<th>Email</th>
|
||||
<th>Phone</th>
|
||||
<th>Twitter</th>
|
||||
<th colspan="3"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @friends.each do |friend| %>
|
||||
<tr>
|
||||
<td><%= friend.first_name %></td>
|
||||
<td><%= friend.last_name %></td>
|
||||
<td><%= friend.email %></td>
|
||||
<td><%= friend.phone %></td>
|
||||
<td><%= friend.twitter %></td>
|
||||
<td><%= link_to 'Show', friend %></td>
|
||||
<td><%= link_to 'Edit', edit_friend_path(friend) %></td>
|
||||
<td><%= link_to 'Destroy', friend, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<br>
|
||||
|
||||
<%= link_to 'New Friend', new_friend_path %>
|
|
@ -0,0 +1 @@
|
|||
json.array! @friends, partial: "friends/friend", as: :friend
|
|
@ -0,0 +1,5 @@
|
|||
<h1>Add New Friend</h1>
|
||||
|
||||
<%= render 'form', friend: @friend %>
|
||||
|
||||
<%= link_to 'Back', friends_path %>
|
|
@ -0,0 +1,29 @@
|
|||
<p id="notice"><%= notice %></p>
|
||||
|
||||
<p>
|
||||
<strong>First name:</strong>
|
||||
<%= @friend.first_name %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Last name:</strong>
|
||||
<%= @friend.last_name %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Email:</strong>
|
||||
<%= @friend.email %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Phone:</strong>
|
||||
<%= @friend.phone %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Twitter:</strong>
|
||||
<%= @friend.twitter %>
|
||||
</p>
|
||||
|
||||
<%= link_to 'Edit', edit_friend_path(@friend) %> |
|
||||
<%= link_to 'Back', friends_path %>
|
|
@ -0,0 +1 @@
|
|||
json.partial! "friends/friend", friend: @friend
|
|
@ -16,6 +16,18 @@
|
|||
|
||||
</li>
|
||||
|
||||
<li class="nav-item">
|
||||
|
||||
<%= link_to "Friends", friends_path, class:"nav-link" %>
|
||||
|
||||
</li>
|
||||
|
||||
<li class="nav-item">
|
||||
|
||||
<%= link_to "Add Friend", new_friend_path, class:"nav-link" %>
|
||||
|
||||
</li>
|
||||
|
||||
|
||||
</ul>
|
||||
<form class="d-flex" role="search">
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
Rails.application.routes.draw do
|
||||
resources :friends
|
||||
# get 'home/index' # mj
|
||||
get 'home/about'
|
||||
root 'home#index'
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
class CreateFriends < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :friends do |t|
|
||||
t.string :first_name
|
||||
t.string :last_name
|
||||
t.string :email
|
||||
t.string :phone
|
||||
t.string :twitter
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,25 @@
|
|||
# This file is auto-generated from the current state of the database. Instead
|
||||
# of editing this file, please use the migrations feature of Active Record to
|
||||
# incrementally modify your database, and then regenerate this schema definition.
|
||||
#
|
||||
# This file is the source Rails uses to define your schema when running `bin/rails
|
||||
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
|
||||
# be faster and is potentially less error prone than running all of your
|
||||
# migrations from scratch. Old migrations may fail to apply correctly if those
|
||||
# migrations use external dependencies or application code.
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2023_12_21_174502) do
|
||||
|
||||
create_table "friends", force: :cascade do |t|
|
||||
t.string "first_name"
|
||||
t.string "last_name"
|
||||
t.string "email"
|
||||
t.string "phone"
|
||||
t.string "twitter"
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,48 @@
|
|||
require "test_helper"
|
||||
|
||||
class FriendsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@friend = friends(:one)
|
||||
end
|
||||
|
||||
test "should get index" do
|
||||
get friends_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get new" do
|
||||
get new_friend_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create friend" do
|
||||
assert_difference('Friend.count') do
|
||||
post friends_url, params: { friend: { email: @friend.email, first_name: @friend.first_name, last_name: @friend.last_name, phone: @friend.phone, twitter: @friend.twitter } }
|
||||
end
|
||||
|
||||
assert_redirected_to friend_url(Friend.last)
|
||||
end
|
||||
|
||||
test "should show friend" do
|
||||
get friend_url(@friend)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get edit" do
|
||||
get edit_friend_url(@friend)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should update friend" do
|
||||
patch friend_url(@friend), params: { friend: { email: @friend.email, first_name: @friend.first_name, last_name: @friend.last_name, phone: @friend.phone, twitter: @friend.twitter } }
|
||||
assert_redirected_to friend_url(@friend)
|
||||
end
|
||||
|
||||
test "should destroy friend" do
|
||||
assert_difference('Friend.count', -1) do
|
||||
delete friend_url(@friend)
|
||||
end
|
||||
|
||||
assert_redirected_to friends_url
|
||||
end
|
||||
end
|
|
@ -0,0 +1,15 @@
|
|||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
first_name: MyString
|
||||
last_name: MyString
|
||||
email: MyString
|
||||
phone: MyString
|
||||
twitter: MyString
|
||||
|
||||
two:
|
||||
first_name: MyString
|
||||
last_name: MyString
|
||||
email: MyString
|
||||
phone: MyString
|
||||
twitter: MyString
|
|
@ -0,0 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class FriendTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -0,0 +1,51 @@
|
|||
require "application_system_test_case"
|
||||
|
||||
class FriendsTest < ApplicationSystemTestCase
|
||||
setup do
|
||||
@friend = friends(:one)
|
||||
end
|
||||
|
||||
test "visiting the index" do
|
||||
visit friends_url
|
||||
assert_selector "h1", text: "Friends"
|
||||
end
|
||||
|
||||
test "creating a Friend" do
|
||||
visit friends_url
|
||||
click_on "New Friend"
|
||||
|
||||
fill_in "Email", with: @friend.email
|
||||
fill_in "First name", with: @friend.first_name
|
||||
fill_in "Last name", with: @friend.last_name
|
||||
fill_in "Phone", with: @friend.phone
|
||||
fill_in "Twitter", with: @friend.twitter
|
||||
click_on "Create Friend"
|
||||
|
||||
assert_text "Friend was successfully created"
|
||||
click_on "Back"
|
||||
end
|
||||
|
||||
test "updating a Friend" do
|
||||
visit friends_url
|
||||
click_on "Edit", match: :first
|
||||
|
||||
fill_in "Email", with: @friend.email
|
||||
fill_in "First name", with: @friend.first_name
|
||||
fill_in "Last name", with: @friend.last_name
|
||||
fill_in "Phone", with: @friend.phone
|
||||
fill_in "Twitter", with: @friend.twitter
|
||||
click_on "Update Friend"
|
||||
|
||||
assert_text "Friend was successfully updated"
|
||||
click_on "Back"
|
||||
end
|
||||
|
||||
test "destroying a Friend" do
|
||||
visit friends_url
|
||||
page.accept_confirm do
|
||||
click_on "Destroy", match: :first
|
||||
end
|
||||
|
||||
assert_text "Friend was successfully destroyed"
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue