Skip to content

Commit

Permalink
Refs issue #12: Add a last name cannot be blank error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Brand committed Nov 3, 2017
1 parent 5154c1c commit f409df7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
9 changes: 7 additions & 2 deletions app/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ def show
end

def new
@author = Author.new
end

def create
@author = Author.new(author_params)
@author.save
redirect_to @author
if @author.save
redirect_to @author
else
render 'new'
end
end

private
Expand Down
3 changes: 1 addition & 2 deletions app/models/author.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class Author < ActiveRecord::Base
validates :last_name, presence: true,
length: { minimum: 1 }
validates :last_name, presence: true
def name
first_name + ' ' + last_name
end
Expand Down
15 changes: 15 additions & 0 deletions app/views/authors/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
<%= form_for :author, url: authors_path do |f| %>

<% if @author.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@author.errors.count, "error") %>
prohibits the author to be created
</h2>
<ul>
<% @author.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<p>
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
Expand Down
2 changes: 1 addition & 1 deletion spec/features/author/new_author_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

find('input[type="submit"]').click

expect(page).to have_text('last name can\'t be blank')
expect(page).to have_text('Last name can\'t be blank')
end

end

0 comments on commit f409df7

Please sign in to comment.