Skip to content

Instantly share code, notes, and snippets.

@kmelkon
Last active December 22, 2015 06:58
Show Gist options
  • Save kmelkon/6434523 to your computer and use it in GitHub Desktop.
Save kmelkon/6434523 to your computer and use it in GitHub Desktop.
<%= form_for [@company, @activity], :remote=> true, :validate=>true do |f| %>
<%= f.label :title %>
<%= f.text_field :title, :class => "input-width bottom-border"%>
<%= f.label :activity_category_id %>
<%= f.collection_select(:activity_category_id, ActivityCategory.all, :id, :activity_category) %>
<%= f.label :background %>
<%= f.text_area :background, :class => "area-height area-width bottom-border" %>
<%= f.submit "Add activity", class: "btn submit-deals btn-info" %>
<% end %>
class ActivitiesController < ApplicationController
def create
@company = Company.find(params[:company_id])
@activity = @company.activities.build(params[:activity])
flash[:success] = 'Activity Created!'
respond_to do |format|
if @activity.save
format.html
format.js
else
render :json => { }, status => 500
end
end
end
end
class Activity < ActiveRecord::Base
attr_accessible :activity_category_id, :background, :title, :user_id
belongs_to :company
belongs_to :contact
belongs_to :activity_category
end
class ActivityCategory < ActiveRecord::Base
attr_accessible :activity_category
has_many :activities
end
Activity Load (0.1ms) SELECT "activities".* FROM "activities" ORDER BY "activities"."id" DESC LIMIT 1
=> #<Activity id: 3, title: "activity", background: "aa", activity_category_id: 2, user_id: nil, created_at: "2013-09-04 09:03:34", updated_at: "2013-09-04 09:03:34", company_id: 1, contact_id: nil>
1.9.3p448 :002 > ActivityCategory.last
ActivityCategory Load (0.3ms) SELECT "activity_categories".* FROM "activity_categories" ORDER BY "activity_categories"."id" DESC LIMIT 1
=> #<ActivityCategory id: 3, activity_category: "Phone Call", created_at: "2013-09-04 08:49:54", updated_at: "2013-09-04 08:49:54">
1.9.3p448 :003 >
Started POST "/companies/1/activities" for 127.0.0.1 at 2013-09-04 12:03:34 +0300
Processing by ActivitiesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Nhflt/o9HdfsWXpsSaKddd5yvItOsGtDoFD5N/Rbfns=", "activity"=>{"title"=>"activity", "activity_category_id"=>"2", "background"=>"aa"}, "commit"=>"Add activity", "company_id"=>"1"}
Company Load (0.1ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = ? LIMIT 1 [["id", "1"]]
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "activities" ("activity_category_id", "background", "company_id", "contact_id", "created_at", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["activity_category_id", 2], ["background", "aa"], ["company_id", 1], ["contact_id", nil], ["created_at", Wed, 04 Sep 2013 09:03:34 UTC +00:00], ["title", "activity"], ["updated_at", Wed, 04 Sep 2013 09:03:34 UTC +00:00], ["user_id", nil]]
(201.5ms) commit transaction
Rendered activities/_activity.html.erb (0.0ms)
Rendered activities/create.js.erb (0.7ms)
Completed 200 OK in 212ms (Views: 2.9ms | ActiveRecord: 202.1ms)
["Meeting", "Email", "Phone Call"].each do |category|
ActivityCategory.create(:activity_category => category)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment