我有以下要求:模型叫医疗;它有医生类型和治疗类型;可以为医学模型选择多种医生类型和治疗类型。
我试图避免为医生类型和治疗类型创建模型。它们基本上是应用程序为其用户提供的集合。我试图将它们作为hstore字段存储在Medical模型中。
我做了以下......
为hstore添加了迁移:
class AddHstore < ActiveRecord::Migration
def up
enable_extension :hstore
end
def down
disable_extension :hstore
end
end
添加了迁移以添加hstore字段:
class AddDataToMedical < ActiveRecord::Migration
def change
add_column :medical, :data, :hstore
end
end
在模型中添加了以下内容:
class Medical < ActiveRecord::Base
DOCTOR_TYPE = ['MD', 'Specialist', 'DO', 'Other']
TREATMENT_TYPE = ['PT', 'Chiro', 'Meds', 'Other']
store_accessor :data, :doctor_type, :treatment_type
...
在strong_params中添加了以下内容:
# Never trust parameters from the scary internet, only allow the white list through.
def medical_params
params.require(:medical).permit(:total_med_bills, :subrogated_amount, :injuries_within_three_days, :length_of_treatment,
:length_of_treatment_unit, :injury_summary, :medical_summary,
:earnings_lost, :treatment_gap, :injections, :hospitalization, :hospital_stay_length,
:hospital_stay_length_unit, :data => [:doctor_type, :treatment_type], :injuries_attributes => [:injury_type, :region, :code, :dominant_side, :joint_fracture,
:displaced_fracture, :disfigurement, :impairment, :permanence, :disabled,
:disabled_percent, :surgery, :surgery_count, :surgery_type, :casted_fracture,
:stitches, :future_surgery, :future_medicals, :id])
end
然后将新字段添加到表单中:
<%= simple_form_for([@case, @medical]) do |f| %>
<%= f.input :total_med_bills %>
<%= f.input :length_of_treatment %>
<%= f.simple_fields_for :data do |d| %>
<%= d.input :doctor_type, as: :check_boxes, collection: Medical::DOCTOR_TYPE %>
<%= d.input :treatment_type, as: :check_boxes, collection: Medical::TREATMENT_TYPE %>
<% end %>
<%= f.input :injury_summary %>
...
问题是当我在medicals_controller的Update方法中执行put params [:data]时,为什么数据参数为空(我只在编辑中使用此字段)?另外,为什么我在我的控制台中获得以下内容:
Unpermitted parameters: doctor_type, treatment_type
当我查看我的控制台时,我发现数据哈希不是空的:
..."length_of_treatment"=>"", "data"=>{"doctor_type"=>["MD", "Specialist", ""], "treatment_type"=>["Meds", "Other", ""]}, "injury_summary"=>"",...
答案 0 :(得分:1)
由于强参数不允许强参数中的标量值...我最终执行了以下操作...
强参数:
... :data => [:doctor_type => [], :treatment_type => []] ...
doctor_type
和treatment_type
个键可以有多个值。