Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feedback Messages when fields are not filled in #15

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ def new
end

def create
if Person.create(person_attributes)
@person = Person.new(person_attributes)
if @person.save
redirect_to people_path, notice: 'Successfully created entry'
else
render :create, alert: 'Unsuccessfully created entry'
render :new, alert: 'Unsuccessfully created entry', status: :unprocessable_entity
end
end

private

def person_attributes
params.require(:person).permit(:name, :email, :phone)
params.require(:person).permit(:name, :email, :phone_number)
end

end
Expand Down
4 changes: 4 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
class Person < ApplicationRecord

belongs_to :company, optional: true

validates :name, presence: true
validates :phone_number, presence: true
validates :email, presence: true
end
2 changes: 1 addition & 1 deletion app/views/people/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ table.table
tr
th[scope="row"]= person&.id
td= person.try(:name)
td= person.try(:phone)
td= person.try(:phone_number)
td= person.try(:email)
td= person.try(:company).try(:name)

Expand Down
13 changes: 10 additions & 3 deletions app/views/people/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
h2 Creating an entry

= form_for @person, class: 'row' do |f|
- if @person.errors.any?
.col-12
.alert.alert-danger
h4.alert-heading Form contains errors
ul
- @person.errors.full_messages.each do |message|
li= message
.col-auto
= f.label :name, class: 'form-label'
= f.text_field :name, class: 'form-control'
= f.text_field :name, class: "form-control #{'is-invalid' if @person.errors[:name].any?}"
.col-auto
= f.label :phone_number, class: 'form-label'
= f.text_field :phone_number, class: 'form-control'
= f.text_field :phone_number, class: "form-control #{'is-invalid' if @person.errors[:phone_number].any?}"
.col-auto
= f.label :email, class: 'form-label'
= f.text_field :email, class: 'form-control'
= f.text_field :email, class: "form-control #{'is-invalid' if @person.errors[:email].any?}"
.col-auto.mt-4
= f.submit class: 'btn btn-primary'
4 changes: 4 additions & 0 deletions spec/controllers/people_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
it 'has status found' do
expect(post :create, params: { person: { name: 'foo', phone_number: '123', email: 'foo' } }).to have_http_status(:found)
end

it 'doesn\'t create a user when is missing phone_number' do
expect(post :create, params: { person: { name: 'foo', email: 'foo' } }).to have_http_status(:unprocessable_entity)
end
end
end
5 changes: 5 additions & 0 deletions spec/factories/companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :company do
name { "Company default" }
end
end
7 changes: 7 additions & 0 deletions spec/factories/people.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :person do
name { "John Doe" }
email { "[email protected]" }
phone_number { "9770607060" }
end
end
15 changes: 13 additions & 2 deletions spec/features/people/index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'rails_helper'

RSpec.describe 'Listing people', type: :feature do
before do
Person.create(
before do
FactoryBot.create(
:person,
name: 'Foo Bar',
phone_number: 'Biz',
email: 'Baz'
Expand All @@ -24,4 +25,14 @@
expect(page).to have_field :person_name
end

scenario 'New person with errors', type: :feature do
visit new_person_path

fill_in :person_name, with: 'John Doe'

click_on 'Create Person'

expect(page).to have_content("Email can't be blank")
expect(page).to have_content("Phone number can't be blank")
end
end
14 changes: 13 additions & 1 deletion spec/views/people/index.html.slim_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
require "rails_helper"

describe "people/index.html.slim" do
it "Displays the users"
let(:company) { FactoryBot.create(:company, name: "Default Company") }
let(:first_person) { FactoryBot.create(:person, phone_number: "Foo", company: company) }
let(:second_person) { FactoryBot.create(:person, name: "Jane Doe", email: "[email protected]", phone_number: "Bar", company: company) }
it "Displays the users" do
assign(:people, [first_person, second_person])
render

expect(rendered).to match(/John/) # name (John)
expect(rendered).to match(/Foo/) # phone number
expect(rendered).to match(/Jane/) # name (Jane)
expect(rendered).to match(/Bar/) # phone number
expect(rendered).to match(/Default Company/) # company name
end
end