Develop the right mindset for Rails security
Avoid shipping vulnerable code by learning how to prevent security issues in your Rails applications.
Get the course for $99# app/models/post.rb
class Post < ActiveRecord::Base
validates_presence_of :title, :body
end# test/models/post_test.rb
require 'test_helper'
class PostTest < ActiveSupport::TestCase
test "should not save post without title or body" do
post = Post.new
assert_not post.save
post.title = 'Test'
assert_not post.save
post.body = 'Test body'
assert post.save
end
end# test/models/post_test.rb
test "should have the necessary required validators" do
post = Post.new
assert_not post.valid?
assert_equal [:title, :body], post.errors.keys
end# app/models/post.rb
class Post < ActiveRecord::Base
validates_presence_of :title, :body
validates :score, numericality: true, allow_blank: true
end# test/models/post_test.rb
test "should have numeric score" do
post = Post.new(title: 'test', body: 'test body', score: 'test')
assert_not post.valid?
assert_equal ["is not a number"], post.errors.messages[:score]
end