Passing a reference to a form gives key :class_blueprint not found in: %{"class_blueprint" => "3"}

Can someone please tell me why this isn’t working and what I need to do to make it work…

# key :class_blueprint not found in: %{"class_blueprint" => "3"}
# lib/open_teaching_platform_web/controllers/scheduled_class_controller.ex

render(conn, "index.html", scheduled_classes: scheduled_classes)
end

def new(conn, params) do
  changeset = Classes.change_scheduled_class(%ScheduledClass{})
  class_blueprint = params.class_blueprint
  render(conn, "new.html", changeset: changeset, class_blueprint: class_blueprint)
end

def create(conn, %{"scheduled_class" => scheduled_class_params}) do
  case Classes.create_scheduled_class(scheduled_class_params) do

There is no key :class_blueprint in your map, you need to use a string to access the value.

Even better were to normalize it into a struct or another data structure with a well known and validated shape.

3 Likes

This is your clue. You are accessing an atom map key while the map has the same key but in string form. Consider this:

map = %{"a" => 1}
x = map[:a] # this will yield nil
y = map.a # this will error like in your example
z = map["a"] # this will return 1 as expected

So basically, class_blueprint = params.class_blueprint fails because you don’t actually have a :class_blueprint key in your map. You only have a "class_blueprint" key.

1 Like