Skip to content

Instantly share code, notes, and snippets.

@jamesgecko
Created February 28, 2012 15:15
Show Gist options
  • Save jamesgecko/1933035 to your computer and use it in GitHub Desktop.
Save jamesgecko/1933035 to your computer and use it in GitHub Desktop.
<%= form_for([@customer, @phone]) do |f| %>
<% if @phone.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@phone.errors.count, "error") %> prohibited this phone from being saved:</h2>
<ul>
<% @phone.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :number %><br />
<%= f.number_field :number %>
</div>
<div class="field">
<%= f.label :type %><br />
<%= f.number_field :type %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Customer < ActiveRecord::Base
has_many :phones
accepts_nested_attributes_for :phones, :allow_destroy => true
end
class Phone < ActiveRecord::Base
belongs_to :customer
attr_accessible :number, :type
#validates :number, presence: true, length: { is: 10 }
end
class PhonesController < ApplicationController
# GET /phones
# GET /phones.json
def index
@phones = Phone.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @phones }
end
end
# GET /phones/1
# GET /phones/1.json
def show
@phone = Phone.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @phone }
end
end
# GET /phones/new
# GET /phones/new.json
def new
@phone = Phone.new
@customer = Customer.find(params[:customer_id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: @phone }
end
end
# GET /phones/1/edit
def edit
@phone = Phone.find(params[:id])
end
# POST /phones
# POST /phones.json
def create
@phone = Phone.new(params[:phone])
@customer = Customer.find(params[:customer_id])
respond_to do |format|
if @phone.save
format.html { redirect_to @customer, notice: 'Phone was successfully created.' }
format.json { render json: @phone, status: :created, location: @phone }
else
format.html { render action: "new" }
format.json { render json: @phone.errors, status: :unprocessable_entity }
end
end
end
# PUT /phones/1
# PUT /phones/1.json
def update
@phone = Phone.find(params[:id])
respond_to do |format|
if @phone.update_attributes(params[:phone])
format.html { redirect_to @customer, notice: 'Phone was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @phone.errors, status: :unprocessable_entity }
end
end
end
# DELETE /phones/1
# DELETE /phones/1.json
def destroy
@phone = Phone.find(params[:id])
@phone.destroy
respond_to do |format|
format.html { redirect_to @customer }
format.json { head :no_content }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment