Created
December 10, 2013 00:04
-
-
Save sauron/7883410 to your computer and use it in GitHub Desktop.
Acciones especificas para un recurso/miembro en particular.
Manejo de sessiones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
#agregancdo una accion a un recurso/miembro en particular | |
resources :books do | |
member do | |
put :publish | |
end | |
end | |
# o | |
put "/books/:id/publish", to: "books#publish" | |
# Genera: rake routes | |
publish_book PUT "/books/:id/publish" "books#publish" | |
#los links se generarian asi: | |
link_to "Publicar", publish_book(@book) | |
#Generar migraciones para agregar campos de publicacion | |
rails g migration AddPublicationWorkflowToBooks | |
#genera la migracion donde agregamos los campos: | |
class AddPublicationWorkflowToBooks | |
def change | |
add_column :books, :publish_date, :date_time | |
add_column :books, :publish_state, :string | |
add_index :books, :publish_date | |
end | |
end | |
##Sesiones | |
#Como mantener seleccionado el ultimo autor. | |
#app/views/books/_form.html.erb | |
<div class="field"> | |
<%= f.label :author_id, "Author" %><br> | |
<%= f.select :author_id, options_for_select(Author.all.map{|a| [a.fullname, a.id] }, @author_id), { include_blank: true } %> | |
</div> | |
#app/controllers/books_controller.rb | |
def new | |
@author_id = session[:author_id] #recupero de session el ultimo author elegido | |
@book = Book.new | |
render :new | |
end | |
def create | |
@book = Book.new(book_params) | |
@book.save | |
... | |
session[:author_id] = @book.author_id #preservo en session el ultimo autor elegido | |
... | |
render :show | |
end | |
def publish | |
@book = Book.find(params[:id]) | |
@book.publish! | |
render :show | |
end | |
#app/models/book.rb | |
def publish! | |
self.publish_date = Time.now | |
self.publish_state = "Published" | |
#Truquito, les deje la inquietud de que corroboren que el book queda publicado 😉 | |
end | |
def published? | |
publish_date.present? #alternativa 1 | |
publish_state == "Published" #alternativa 2 | |
end | |
#Recurso para publicar la app en heroku | |
https://devcenter.heroku.com/articles/getting-started-with-rails4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment