Skip to content

Instantly share code, notes, and snippets.

@EminenceHC
Last active November 20, 2015 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EminenceHC/f82439550801bd2aef39 to your computer and use it in GitHub Desktop.
Save EminenceHC/f82439550801bd2aef39 to your computer and use it in GitHub Desktop.
On an edit page, attach additional fields_for to a nested objec
class CaseManagement < ActiveRecord::Base
has_one :service, as: :serviceable
has_many :treatment_plan_problems, dependent: :destroy
accepts_nested_attributes_for :treatment_plan_problems, allow_destroy: true
validates_associated :treatment_plan_problems
end
class CaseManagementsController < ApplicationController
def edit
@case_management = CaseManagement.find(params[:id])
end
def update
@case_management = CaseManagement.find(params[:id])
if @case_management.update_attributes(case_management_params)
redirect_to @case_management, notice: "Successfully updated case management."
else
render :edit
end
end
def show
@case_management = CaseManagement.find(params[:id])
end
def case_management_params
params.require(:case_management).permit(:service_id, :new_problem, :counselor_id, :student_id, :stage, treatment_plan_problems_attributes: [:id, :initial, :domain, :treatment_plan_id, :problem_id, :problem_identifier, :problem_status, :problem_resolved, :client_statement, :severity, :severity_change, :current_severity, :_destroy, treatment_plan_problem_plan_attributes: [:treatment_plan_problem_id, :status, :resolved, :resolved_methods, :resolved_triggers, :continue, :prevented, :barriers, :this_time, :differently, :goal_change, :new_problem, :_destroy]])
end
end
<%= simple_form_for @case_management, html: {class: 'form-horizontal'} do |f| %>
<%= f.simple_fields_for :treatment_plan_problems do |problem| %>
<h4><%= Problem.find(problem.object.problem_identifier).item %></h4>
<hr>
<h5><%= problem.object.domain %></h5>
<table>
<tbody>
<tr>
<td>Client's statement of the problem</td>
<td><%= problem.object.client_statement %></td>
</tr>
<tr>
<td>Severity</td>
<td><%= problem.object.current_severity %></td>
</tr>
</tbody>
</table>
<%= f.simple_fields_for :treatment_plan_problem_plan do |plan| %>
# THE FOLLOWING FIELD :status SHOULD BE ADDED TO A TREATMENT PLAN PROBLEM BUT DOES NOT SHOW UP IN PARAMS HASH. I feel like the above line should be problem.simple_fields_for instead of f.simple_fields_for, but then the following :status field does not display on the form.
<%= plan.input :status, label: 'What is the current status of the problem?', collection: ['Problem has been resolved', 'Problem still exists, continue with same plan', 'Problem still exists, change goals and methods'], input_html: {class: 'problem-plan-status'} %>
<% end %>
<% end %>
class TreatmentPlanProblem < ActiveRecord::Base
belongs_to :case_management
has_one :treatment_plan_problem_plan, dependent: :destroy
accepts_nested_attributes_for :treatment_plan_problem_plan, allow_destroy: true
validates_associated :treatment_plan_goals
validates_associated :treatment_plan_problem_plan
end
class TreatmentPlanProblemPlan < ActiveRecord::Base
belongs_to :treatment_plan_problem
validates :status, presence: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment