Skip to content

Commit

Permalink
Refs #12: show validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBrendel committed Nov 3, 2017
1 parent ce9aa18 commit c5fe05e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ def show
@author = Author.find(params[:id])
end

def new; end
def new
@author = Author.new
end

def create
@author = Author.new(params.require(:author).permit(:first_name, :last_name, :homepage))

@author.save
redirect_to @author
if @author.save
redirect_to @author
else
render 'new'
end
end
end
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') %> prohibited
this author from being saved:
</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

0 comments on commit c5fe05e

Please sign in to comment.