diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb
new file mode 100644
index 0000000..74703cd
--- /dev/null
+++ b/app/controllers/books_controller.rb
@@ -0,0 +1,23 @@
+class BooksController < ApplicationController
+ def new
+ @books = Book.new
+ end
+
+ def create
+ @books = Book.new(book_params)
+ if @books.save
+ flash[:info] = I18n.t('es.flash.book.created')
+ redirect_to @books
+ else
+ render 'new'
+ end
+ end
+
+ def show
+ @books = Book.find(params[:id])
+ end
+
+ def book_params
+ params.require(:book).permit(:title)
+ end
+end
\ No newline at end of file
diff --git a/app/helpers/book_helper.rb b/app/helpers/book_helper.rb
new file mode 100644
index 0000000..4872edd
--- /dev/null
+++ b/app/helpers/book_helper.rb
@@ -0,0 +1,2 @@
+module BookHelper
+end
\ No newline at end of file
diff --git a/app/views/books/new.html.erb b/app/views/books/new.html.erb
new file mode 100644
index 0000000..a556bc4
--- /dev/null
+++ b/app/views/books/new.html.erb
@@ -0,0 +1,9 @@
+
+ <%= form_for(@books) do |f| %>
+ <%= render 'shared/error_messages', object: f.object %>
+ <%= f.label :title %>
+ <%= f.text_field :title, placeholder: true %>
+
+ <%= f.submit %>
+ <% end %>
+
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb
new file mode 100644
index 0000000..1196a1f
--- /dev/null
+++ b/app/views/books/show.html.erb
@@ -0,0 +1,4 @@
+<% flash.each do |message_type, message| %>
+ <%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
+<% end %>
+<%= @books.title %>
diff --git a/app/views/static_pages/home.html.erb b/app/views/static_pages/home.html.erb
index a3617eb..f9c5b88 100644
--- a/app/views/static_pages/home.html.erb
+++ b/app/views/static_pages/home.html.erb
@@ -1,5 +1,6 @@
Recetame App
<%= link_to t('button.recipe.create'), new_recipe_path %>
+<%= link_to t('button.book.create'), new_book_path %>
Aquí están las últimas 10 recetas
diff --git a/config/locales/es.yml b/config/locales/es.yml
index c7ea492..91bb9c7 100644
--- a/config/locales/es.yml
+++ b/config/locales/es.yml
@@ -21,14 +21,24 @@
es:
flash:
+ book:
+ created: "Su libro se ha creado"
recipe:
created: "Su receta se ha creado"
button:
+ book:
+ create: "Crear libro"
recipe:
create: "Crear receta"
activerecord:
errors:
models:
+ book:
+ attributes:
+ title:
+ too_short: "Titulo demasiado corto"
+ too_long: "Titulo demasiado largo"
+ blank: "Titulo en blanco, inserta un titulo"
recipe:
attributes:
title:
@@ -40,14 +50,21 @@ es:
too_long: "Demasiado largo"
blank: "Escribe algo"
models:
+ book:
+ one: "Libro"
+ other: "Libros"
recipe:
one: "Receta"
other: "Recetas"
attributes:
+ book:
+ title: "Nombre del Libro"
recipe:
- title: "Nombre Receta"
+ title: "Nombre de la Receta"
helpers:
placeholder:
+ book:
+ title: "Escribe un título"
recipe:
title: "Escribe un título"
submit:
diff --git a/config/routes.rb b/config/routes.rb
index acc194b..c4ac468 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,7 +1,7 @@
Rails.application.routes.draw do
root 'static_pages#home'
#get 'recipes/new' => 'recipes#new'
- resources :recipes
+ resources :recipes, :books
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
diff --git a/test/integration/books_test.rb b/test/integration/books_test.rb
new file mode 100644
index 0000000..34fe8f4
--- /dev/null
+++ b/test/integration/books_test.rb
@@ -0,0 +1,23 @@
+require 'test_helper'
+
+class BooksTest < ActionDispatch::IntegrationTest
+ test "invalid new book information" do
+ get new_book_path
+ assert_no_difference 'Book.count' do
+ post books_path, book: { title: "" }
+ end
+ assert_select 'div.alert-danger'
+ assert_template 'books/new'
+ end
+
+ test "valid new book information" do
+ get new_book_path
+ assert_difference 'Book.count', 1 do
+ post books_path, book: { title: "Arroces del mundo" }
+ end
+ follow_redirect!
+ assert_not flash.empty?
+ assert_select 'div.alert-info'
+ assert_template 'books/show'
+ end
+end
diff --git a/test/models/book_test.rb b/test/models/book_test.rb
index b7265a8..0e3ea57 100644
--- a/test/models/book_test.rb
+++ b/test/models/book_test.rb
@@ -2,6 +2,28 @@
class BookTest < ActiveSupport::TestCase
+ def setup
+ @books = Book.new(title: "Curry con todo")
+ end
+
+ test "title should be present" do
+ @books.title = " "
+
+ assert_not @books.valid?
+ end
+
+ test "title should not be too short" do
+ @books.title = "a"*4
+
+ assert_not @books.valid?
+ end
+
+ test "title should not be too long" do
+ @books.title = "a"*101
+
+ assert_not @books.valid?
+ end
+
test "last_five should have 5 items" do
if Book.all.size >= 5
assert_equal Book.last_five.size, 5