ruby on rails - ActiveModel::MissingAttributeError in Controller can't write unknown attribute -
for 1 of views in rails application, have set controller such. want students records db , append values each student. giving me error:
activemodel::missingattributeerror in memomaintestercontroller#test_students can't write unknown attribute current_target
class memomaintestercontroller < applicationcontroller def test_students @all_students = student.all @all_students.each |student| current = current_target(student) previous_test_info = last_pass(student) student[:current_target] = current[0] student[:current_level] = current[1] student[:current_target_string] = "level #{current[0]} - target #{current[1]}" student[:last_pass] = previous_test_info[0] student[:attempts] = previous_test_info[1] student[:last_pass_string] = previous_test_info[2] end end . . . end
it occurs student[:current_target] = current[0]
.
am not allowed append values hash? there workaround this?
edit: although student.all
model instance, want turn hash , append more key value pairs it.
in case, student
not hash student
model instance.
when call student[:current_target]
attempting write student's current_target
attribute, surely not actual attribute in db students
table. hence error.
to obtain hash models containing data, may consider refactor:
class memomaintestercontroller < applicationcontroller def test_students @all_students = student.all @students_with_steroids = @all_students.map |student| current = current_target(student) previous_test_info = last_pass(student) student_attributes = student.attributes # <= hash, store in student_attributes hash variable student_attributes.merge(current_target: current[0], current_level: current[1], current_target_string: "level #{current[0]} - target #{current[1]}", last_pass: previous_test_info[0], attempts: previous_test_info[1], last_pass_string: previous_test_info[2]) end end
Comments
Post a Comment