Skip to content

Instantly share code, notes, and snippets.

@adrianpike
Created November 1, 2011 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adrianpike/1331817 to your computer and use it in GitHub Desktop.
Save adrianpike/1331817 to your computer and use it in GitHub Desktop.
class Asset < AR
end
class Alert < AR
belongs_to :asset
has_many :logs
accepts_nested_attributes_for :logs
end
class Log < AR
belongs_to :asset
belongs_to :alert
end
###### zee controller
class AlertsController < ApplicationController
def resolve
# TODO: only alerts that need resolution
respond_with {
@alert = @asset.alerts.find(params[:id])
@alert.log_entries.build({:completed_at => Time.now})
}
end
def update
@alert = @asset.alerts.find(params[:id])
@alert.update_attributes(params[:alert])
respond_with(@alert, :location => asset_alerts_path(@asset.becomes(Asset)))
end
end
##### zee view
<%= semantic_form_for([@asset.becomes(Asset), @alert.becomes(Alert)]) do |f| %>
<%= f.inputs do %>
<%= f.semantic_fields_for(:log_entries, @alert.log_entries.last) do |log| %>
<%=log.input :completed_at, :as => :sane_datetime %>
<%=log.input :content %>
<% end %>
<%= f.input :next_due %>
<% end %>
<%=f.buttons %>
<% end %>
##### what I post:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FOOBAR!!!!111one", "alert"=>{"log_attributes"=>{"0"=>{"completed_at(1s)"=>"11/01/2011", "completed_at(2s)"=>"12:00 AM", "content"=>"TESTERY!"}}, "next_due"=>"100"}, "commit"=>"Update Alert", "asset_id"=>"1", "id"=>"1"}
##### what I get:
=> #<Log id: 1, asset_id: nil, issue_id: nil, alert_id: 1, cost: nil, content: "TESTERY!", completed_at: "2011-11-01 07:00:00", created_at: "2011-11-01 20:25:15", updated_at: "2011-11-01 20:25:15">
Any way to set asset_id on the child created log?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment