Skip to content

Show error messages and preserve values in People entry form on failure #19

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

Open
wants to merge 1 commit 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
11 changes: 8 additions & 3 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ 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'
respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace(@person, partial: 'people/form', locals: { person: @person }) }
format.html { render :new }
end
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
2 changes: 2 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
class Person < ApplicationRecord

belongs_to :company, optional: true

validates :name, presence: true
end
17 changes: 17 additions & 0 deletions app/views/people/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
= form_for @person, class: 'row' do |f|
- if @person.errors.any?
.errors.alert-danger.mt-3
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'
.col-auto
= f.label :phone_number, class: 'form-label'
= f.text_field :phone_number, class: 'form-control'
.col-auto
= f.label :email, class: 'form-label'
= f.text_field :email, class: 'form-control'
.col-auto.mt-4
= f.submit class: 'btn btn-primary'
13 changes: 1 addition & 12 deletions app/views/people/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
h2 Creating an entry

= form_for @person, class: 'row' do |f|
.col-auto
= f.label :name, class: 'form-label'
= f.text_field :name, class: 'form-control'
.col-auto
= f.label :phone_number, class: 'form-label'
= f.text_field :phone_number, class: 'form-control'
.col-auto
= f.label :email, class: 'form-label'
= f.text_field :email, class: 'form-control'
.col-auto.mt-4
= f.submit class: 'btn btn-primary'
= render 'form', person: @person
25 changes: 25 additions & 0 deletions spec/features/people/new_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "rails_helper"

RSpec.feature "Create Person", type: :feature do
scenario "User creates a person successfully" do
visit new_person_path

fill_in "Name", with: "Esteban"
fill_in "Phone number", with: "1234567890"
fill_in "Email", with: "[email protected]"

click_button "Create Person"
expect(page).to have_content("Esteban")
end

scenario "User fails to create a person" do
visit new_person_path

fill_in "Name", with: ""
fill_in "Phone number", with: ""
fill_in "Email", with: ""
click_button "Create Person"

expect(page).to have_content("Name can't be blank")
end
end