gem 'prawn'
// Recipie Model
def shopping_list_pdf
shopping_list = ingredients.map do |ingredient|
[ingredient.name, ingredient.quantity]
end
pdf = Prawn::Document.new
pdf.table([[ "Item", "Quantity" ], *shopping_list]) do |t|
t.header = true
t.row_colors = [ "aaaaff", "aaffaa", "ffaaaa" ]
t.row(0).style :background_color => '448844', :text_color => 'ffffff' t.columns(1).align = :right
end
pdf.render
end
// Recipie Controller
def shopping_list
@recipe = Recipe.find(params[:id])
respond_to do |format|
format.html format.pdf do
send_data @recipe.shopping_list_pdf,
content_type: Mime::PDF
end
end
end
// Routes
resources :recipes do
member do
get "shopping_list"
end
resources :ingredients
end