Skip to content

changes on the form and people controller to display error messages #1

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

Merged
merged 1 commit into from
Dec 15, 2024
Merged
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,10 +9,12 @@ 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, status: :unprocessable_entity
end
end

Expand All @@ -21,6 +23,5 @@ def create
def person_attributes
params.require(:person).permit(:name, :email, :phone_number)
end

end

3 changes: 2 additions & 1 deletion app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#

class Person < ApplicationRecord

belongs_to :company, optional: true

validates :name, :phone_number, :email, presence: true
end
19 changes: 19 additions & 0 deletions app/views/people/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
= form_for person, class: 'row' do |f|
- if person.errors.any?
.alert.alert-danger
h4 There were errors with your submission:
ul
- person.errors.full_messages.each do |msg|
li = msg

.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 person.new_record? ? 'Create Person' : 'Update Person', class: 'btn btn-primary'
14 changes: 1 addition & 13 deletions app/views/people/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
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