Skip to content

Commit

Permalink
Company create form and company show
Browse files Browse the repository at this point in the history
  • Loading branch information
viceversus committed Sep 23, 2012
1 parent 9516e39 commit 509220d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 21 deletions.
13 changes: 13 additions & 0 deletions app/controllers/companies_controller.rb
@@ -1,9 +1,22 @@
class CompaniesController < ApplicationController

def new
@company = Company.new
@company.employees.build
end

def create
@company = Company.new(params[:company])
if @company.save
flash[:success] = "Company created successfully."
redirect_to @company
else
render :new
end
end

def show
@company = Company.find(params[:id])
@employees = @company.employees
end
end
4 changes: 3 additions & 1 deletion app/models/company.rb
@@ -1,5 +1,7 @@
class Company < ActiveRecord::Base
attr_accessible :description, :name
attr_accessible :description, :name, :employees_attributes
has_many :employments, as: :employmentable
has_many :employees, through: :employments

accepts_nested_attributes_for :employees, :reject_if => proc { |employee| employee['email'].blank? || employee['password'].blank? || employee['password_confirmation'].blank? }
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -11,7 +11,7 @@ class User < ActiveRecord::Base
has_many :employments
has_many :schools, through: :enrollments
has_many :company_employers, through: :employments, source: :employmentable, source_type: "Company"
has_many :school_employers, through: :employments, source: :employmentable, source_type: "School"
has_many :school_employers, through: :employments, source: :employmentable, source_type: "School", inverse_of: :employees
has_many :links
has_many :skills
end
34 changes: 17 additions & 17 deletions app/views/companies/new.html.slim
@@ -1,31 +1,31 @@
h1 "Hello World"
h1 Hello World

= form_for @company do |f|
-if @company.errors.any?
- if @company.errors.any?
div.error_messages
h2 Form is invalid
ul
- for message in @company.errors.full_messages
li = message

= f.fields_for :employees do |builder|
h3 Company Moderator

f.fields_for :employees, @company.employees do |builder|
builder.label :name
builder.text_field :name
= builder.label :email
= builder.text_field :email

builder.label :email
builder.text_field :email
= builder.label :password
= builder.password_field :password

builder.label :password
builder.password_field :password
= builder.label :password_confirmation
= builder.password_field :password_confirmation

builder.label :password_confirmation
builder.password_field :password_confirmation
h3 Company Info

f.label :name
f.text_field :name
= f.label :name
= f.text_field :name

f.label :name
f.text_field :name
= f.label :description
= f.text_field :description

f.label :name
f.text_field :name
= f.submit "Submit"
11 changes: 11 additions & 0 deletions app/views/companies/show.html.slim
@@ -0,0 +1,11 @@
h1 Shark Week?

h3 Company
= @company.name
br
h3 Description
= @company.description
br
h3 Employees
- @employees.each do |employee|
= employee.email
5 changes: 5 additions & 0 deletions db/migrate/20120923035341_rename_user_id_to_employee_id.rb
@@ -0,0 +1,5 @@
class RenameUserIdToEmployeeId < ActiveRecord::Migration
def change
rename_column :employments, :user_id, :employee_id
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120923005254) do
ActiveRecord::Schema.define(:version => 20120923035341) do

create_table "companies", :force => true do |t|
t.string "name"
Expand All @@ -21,7 +21,7 @@
end

create_table "employments", :force => true do |t|
t.integer "user_id"
t.integer "employee_id"
t.integer "employmentable_id"
t.string "employmentable_type"
t.datetime "created_at", :null => false
Expand Down

0 comments on commit 509220d

Please sign in to comment.