Permalink
Browse files

Add nested agency under User

  • Loading branch information...
Milhouse committed Mar 8, 2012
1 parent c0d9f05 commit cbc7c4991017488050a9d2eaa7978479a3ce0f18
@@ -24,6 +24,7 @@ def show
# GET /agencies/new
# GET /agencies/new.json
def new
+ @user = User.find(params[:user_id])
@agency = Agency.new
respond_to do |format|
@@ -40,8 +41,10 @@ def edit
# POST /agencies
# POST /agencies.json
def create
+ @user = User.find(params[:user_id])
@agency = Agency.new(params[:agency])
-
+ @agency.user_id = @user.id
+
respond_to do |format|
if @agency.save
format.html { redirect_to @agency, notice: 'Agency was successfully created.' }
View
@@ -1,2 +1,3 @@
class Agency < ActiveRecord::Base
+ belongs_to :user
end
View
@@ -13,6 +13,7 @@ class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
+ has_one :agency, dependent: :destroy
before_save :create_remember_token
@@ -1,4 +1,4 @@
-<%= simple_form_for(@agency) do |f| %>
+<%= simple_form_for [@user, @agency] do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
@@ -1,5 +1,5 @@
<h1>New agency</h1>
-<%= render 'form' %>
+<%= render 'form', user: @user %>
-<%= link_to 'Back', agencies_path %>
+<%= link_to 'Back', user_path(@user) %>
@@ -24,6 +24,12 @@
</div>
<% end %>
+<div>
+ <% unless @user.agency %>
+ <%= link_to 'Add Agency Info', new_user_agency_path(@user) %>
+ <% end %>
+</div>
+
<div>
<%= gravatar_for(@user) %>
<a href="http://gravatar.com/emails">change</a>
View
@@ -1,7 +1,11 @@
SampleApp::Application.routes.draw do
+
+resources :agencies
+
+resources :users do
resources :agencies
+end
-resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
@@ -0,0 +1,5 @@
+class AddUserIdToAgencies < ActiveRecord::Migration
+ def change
+ add_column :agencies, :user_id, :integer
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120307044145) do
+ActiveRecord::Schema.define(:version => 20120308020110) do
create_table "agencies", :force => true do |t|
t.integer "federal_id_number"
@@ -29,6 +29,7 @@
t.text "agency_mission"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.integer "user_id"
end
add_index "agencies", ["federal_id_number"], :name => "index_agencies_on_federal_id_number", :unique => true

0 comments on commit cbc7c49

Please sign in to comment.