Skip to content

Instantly share code, notes, and snippets.

@mpron mpron/_form.html.haml
Last active Aug 29, 2015

Embed
What would you like to do?
= form_for @bottles do |f|
- if @bottles.errors.any?
#error_explanation
%h2= "#{pluralize(@bottles.errors.count, "error")} prohibited this wine from being saved:"
%ul
- @bottles.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name, autofocus: true
= f.collection_select :wine_id, Wine.all, :id, :name, class: "form-control input-lg"
OR
.field
= f.label :name
= f.text_field :name, class: "form-control input-lg"
.field
= f.label :vintage
= f.number_field :vintage, class: "form-control input-lg"
.field
= f.label :quantity
= f.number_field :quantity, class: "form-control input-lg"
.field
= f.label :location
= f.number_field :location, class: "form-control input-lg"
.field
= f.label :winery
= f.collection_select :winery_id, Winery.all, :id, :name, class: "form-control input-lg"
.actions
= f.submit 'Save'
ActiveRecord::Schema.define(version: 20140222010849) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "bottles", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "wine_location_id"
end
create_table "locations", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "role", default: "user", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
create_table "wine_locations", force: true do |t|
t.integer "wine_id"
t.integer "location_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "wineries", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "wines", force: true do |t|
t.string "name"
t.integer "vintage"
t.integer "winery_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
%h3
= "#{@user.email}'s Cellar"
%a.dropdown.js-activate-form{href: "#"} Add Wines
.js-form.hide
= render partial: 'wines/form'
%table.table
%tr
%th Location
%th Vintage
%th Wine
%th Winery
%th Quantity
- @wine_locations.each do |wine_loc|
%tr{class: cycle('list_line_odd', 'list_line_even')}
%td= wine_loc.location.name
%td= wine_loc.wine.vintage
%td= wine_loc.wine.name
%td= wine_loc.wine.winery.name
%td= wine_loc.bottles.count
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :wine_locations
def add_bottle(location, wine)
wl = WineLocation.find_or_create_by_wine_id_and_location_id_and_user_id(wine.id, location.id, self.id)
wl.bottles.create()
end
end
class UsersController < ApplicationController
before_action :set_user, :only => [:show, :add_wine]
def index
@users = User.all
end
def show
@wine_locations = @user.wine_locations.all
@quantity = params[:quantity]
@location = params[:location]
@vintage = params[:vintage]
@wine_name = params[:name]
@wine = Wine.find_by_name_and_vintage(@wine_name, @vintage)
@bottles = @user.add_bottle(@location, @wine)
end
def new
end
def create
@quantity.times {@user.add_bottle(@location, @wine)}
end
def add_wine
raise params.inspect
end
private
def set_user
@user = User.find(params[:id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.