Skip to content

Instantly share code, notes, and snippets.

@biographie
Created July 21, 2013 09:41
Show Gist options
  • Save biographie/6048086 to your computer and use it in GitHub Desktop.
Save biographie/6048086 to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: contacts
#
# id :integer not null, primary key
# first_name :text
# last_name :text
# email :text
# bcard :text
# linked_in :text
# background :text
# company_id :integer
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Contact < ActiveRecord::Base
attr_accessible :background, :bcard, :company_id, :email, :first_name, :last_name, :linked_in, :user_id, :phone_numbers_attributes
belongs_to :company
belongs_to :user
accepts_nested_attributes_for :company
has_many :phone_numbers
accepts_nested_attributes_for :phone_numbers
mount_uploader :bcard, FileUploader
end
class ContactsController < ApplicationController
def index
end
def show
@contact = Contact.find(params[:id])
end
def new
@contact = Contact.new
@contact.phone_numbers.build
end
def create
@company = Company.find(params[:company_id])
@contact = @company.contacts.create(params[:contact])
if @contact.save
redirect_to "/contacts"
else
render 'new'
end
end
def edit
end
end
# encoding: utf-8
class FileUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
<div class="bg-color">
<div class="container well">
<h1>Add New Contact</h1>
<%= form_for(@contact,:url => {:action => "create"}, :html => {:multipart => true},) do |f| %>
<%= f.label :first_name %>
<%= f.text_field :first_name, :class => "input-width bottom-border" %>
<%= f.label :last_name %>
<%= f.text_field :last_name, :class => "input-width bottom-border" %>
<div class="items">
<a href="#" class="add">add phone</a>
<%= f.nested_fields_for :phone_numbers do |f|
render 'companies/phone', f: f
end %>
</div>
<%= f.label :email %>
<%= f.text_field :email, :class => "input-width bottom-border" %>
<%= f.label :bcard %>
<%= f.file_field :bcard, :class => "input-width bottom-border" %>
<%= f.label :linked_in %>
<%= f.text_field :linked_in, :class => "input-width bottom-border" %>
<%= f.label :background %>
<%= f.text_area :background, :class => "bg-width text-area-height" %>
<%= f.submit "Add Contact", class: "btn btn-large btn-info" %>
</div>
<% end %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment