Created
January 29, 2011 23:05
-
-
Save andrellima/802299 to your computer and use it in GitHub Desktop.
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
class LivrosController < ApplicationController | |
# GET /livros | |
# GET /livros.xml | |
before_filter :load_generos | |
def index | |
@livros = Livro.all | |
@generos = Genero.all | |
respond_to do |format| | |
format.html # index.html.erb | |
format.xml { render :xml => @livros } | |
end | |
end | |
# GET /livros/1 | |
# GET /livros/1.xml | |
def show | |
@livro = Livro.find(params[:id]) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.xml { render :xml => @livro } | |
end | |
end | |
# GET /livros/new | |
# GET /livros/new.xml | |
def new | |
@livro = Livro.new | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @livro } | |
end | |
end | |
# GET /livros/1/edit | |
def edit | |
@livro = Livro.find(params[:id]) | |
end | |
# POST /livros | |
# POST /livros.xml | |
def create | |
@livro = Livro.new(params[:livro]) | |
respond_to do |format| | |
if @livro.save | |
format.html { redirect_to(@livro, :notice => 'Livro was successfully created.') } | |
format.xml { render :xml => @livro, :status => :created, :location => @livro } | |
else | |
format.html { render :action => "new" } | |
format.xml { render :xml => @livro.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /livros/1 | |
# PUT /livros/1.xml | |
def update | |
@livro = Livro.find(params[:id]) | |
respond_to do |format| | |
if @livro.update_attributes(params[:livro]) | |
format.html { redirect_to(@livro, :notice => 'Livro was successfully updated.') } | |
format.xml { head :ok } | |
else | |
format.html { render :action => "edit" } | |
format.xml { render :xml => @livro.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /livros/1 | |
# DELETE /livros/1.xml | |
def destroy | |
@livro = Livro.find(params[:id]) | |
@livro.destroy | |
respond_to do |format| | |
format.html { redirect_to(livros_url) } | |
format.xml { head :ok } | |
end | |
end | |
protected | |
def load_generos | |
@generos = Genero.all | |
@Genero = Genero.find(params[:genero_id]) if params[:genero_id] | |
end | |
end | |
Livraria::Application.routes.draw do | |
resources :dvds | |
resources :cds | |
resources :livros | |
resource :generos | |
resources :generos do | |
resources :livros | |
end | |
<h3>Categories</h3> | |
<ul class="squared"> | |
<% @generos.each do |genero| %> | |
<li><%= link_to_unless_current genero.nome, livros_path(:genero_id => genero.id) %></li> | |
<% end %> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment