-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create book #5
base: master
Are you sure you want to change the base?
create book #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module BookHelper | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div> | ||
<%= form_for(@books) do |f| %> | ||
<%= render 'shared/error_messages', object: f.object %> | ||
<%= f.label :title %> | ||
<%= f.text_field :title, placeholder: true %> | ||
|
||
<%= f.submit %> | ||
<% end %> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<% flash.each do |message_type, message| %> | ||
<%= content_tag(:div, message, class: "alert alert-#{message_type}") %> | ||
<% end %> | ||
<h1><%= @books.title %></h1> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Vollcode ¿has probado como se muestran estos errores al usuario? Según http://guides.rubyonrails.org/i18n.html
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, voy a echar un vistazo y ver como puedo mejorarlo. |
||
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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parece que las lineas 1 a 3 son las mismas que en app/views/recipes/show.html.erb
¿Se te ocurre como podríamos mejorarlo @Vollcode ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Podríamos hacer un extract method de ello, que te parece @Pardiez ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
me parece que para extraer un "método" habría que tener uno 😉
pero la idea es la misma: evitar esa repetición
De hecho me sé de un tutorial de Rails que te enseña a hacer una aplicación tipo twitter que tiene un ejemplo idéntico, con esas mismas lineas de código 😉 😉