Skip to content

Commit

Permalink
Article validations were added
Browse files Browse the repository at this point in the history
  • Loading branch information
tayfunoziserikan committed Aug 17, 2014
1 parent d1cdbac commit bf6203e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ def create

@article = Article.new(article_params)

@article.save
if @article.save
redirect_to @article
else
render 'new'
end

redirect_to @article

end

Expand Down
2 changes: 2 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Article < ActiveRecord::Base
validates :title, presence: true,
length: { minimum: 5 }
end
13 changes: 13 additions & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
<h1>New Article</h1>

<%= form_for :article, url: articles_path do |f| %>

<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, "error") %> prohibited
this article from being saved:</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
Expand Down

0 comments on commit bf6203e

Please sign in to comment.