Error saving new slected GPG key for a repository

Problem:
When changing the selected GPG key for a repository (in the foreman console), when saving the result, there is an error and the new key is not saved.

An error occurred saving the Repository: undefined methodcontent_id’ for nil:NilClass Did you mean? contains_erb?`

Expected outcome:
the new GPG key is saved and used

Foreman and Proxy versions:
Foreman 1.17.1

Foreman and Proxy plugin versions:
Katello 3.6.1

Other relevant data:
from foreman/production.log

2018-07-30T15:56:43 ca3eaa32 [app] [I] Started PUT "/katello/api/v2/repositories/28212?organization_id=1" for [redacted] at 2018-07-30 15:56:43 +0200
2018-07-30T15:56:43 ca3eaa32 [app] [I] Processing by Katello::Api::V2::RepositoriesController#update as JSON
2018-07-30T15:56:43 ca3eaa32 [app] [I]   Parameters: {"content_type"=>"yum", "docker_upstream_name"=>nil, "mirror_on_sync"=>true, "verify_ssl_on_sync"=>true, "unprotected"=>true, "full_path"=>"http://[redacted]/pulp/repos/[redacted]/Library/custom/Centos_7/Katello_Client_3_6/", "checksum_type"=>"sha256", "container_repository_name"=>nil, "download_policy"=>"immediate", "url"=>"http://fedorapeople.org/groups/katello/releases/yum/3.6/client/el7/x86_64/", "relative_path"=>"[redacted]/Library/custom/Centos_7/Katello_Client_3_6", "major"=>nil, "minor"=>nil, "gpg_key_id"=>39, "content_id"=>"1529577855316", "content_view_version_id"=>1, "library_instance_id"=>nil, "product_type"=>"custom", "promoted"=>true, "ostree_branches"=>[], "upstream_username"=>nil, "ostree_upstream_sync_policy"=>nil, "ostree_upstream_sync_depth"=>nil, "deb_releases"=>nil, "deb_components"=>nil, "deb_architectures"=>nil, "ignore_global_proxy"=>false, "organization"=>{"name"=>"[redacted]", "label"=>"[redacted]", "id"=>1}, "created_at"=>"2018-06-21 10:44:12 UTC", "updated_at"=>"2018-06-21 10:44:40 UTC", "arch"=>"x86_64", "backend_identifier"=>"d12cede6-e46a-424c-b65f-e2edd48aa34c", "content_label"=>nil, "id"=>"28212", "name"=>"Katello Client 3.6", "label"=>"Katello_Client_3_6", "product"=>{"id"=>1, "cp_id"=>"1495022309218", "name"=>"CentOS 7", "sync_plan"=>["name", "description", "sync_date", "interval", "next_sync"]}, "last_sync"=>{"id"=>"02e8d74e-39d9-4191-b9af-be502a0ee5dc", "username"=>"foreman_admin", "started_at"=>"2018-07-30 01:00:27 UTC", "ended_at"=>"2018-07-30 01:00:35 UTC", "state"=>"stopped", "result"=>"success", "progress"=>1}, "content_counts"=>{"ostree_branch"=>0, "docker_manifest"=>0, "docker_manifest_list"=>0, "docker_tag"=>0, "rpm"=>17, "srpm"=>0, "package"=>17, "package_group"=>1, "erratum"=>0, "puppet_module"=>0, "file"=>0, "deb"=>0}, "last_sync_words"=>"about 13 hours", "gpg_key"=>{"name"=>"RPM-GPG-KEY-CentOS-7", "id"=>1}, "environment"=>{"id"=>1}, "permissions"=>{"deletable"=>false}, "upstream_password_exists"=>"[FILTERED]", "upstream_password"=>"[FILTERED]", "organization_id"=>"1", "api_version"=>"v2", "repository"=>{"id"=>"28212", "name"=>"Katello Client 3.6", "created_at"=>"2018-06-21 10:44:12 UTC", "updated_at"=>"2018-06-21 10:44:40 UTC", "major"=>nil, "minor"=>nil, "gpg_key_id"=>39, "library_instance_id"=>nil, "content_id"=>"1529577855316", "arch"=>"x86_64", "label"=>"Katello_Client_3_6", "content_view_version_id"=>1, "relative_path"=>"[redacted]/Library/custom/Centos_7/Katello_Client_3_6", "url"=>"http://fedorapeople.org/groups/katello/releases/yum/3.6/client/el7/x86_64/", "unprotected"=>true, "content_type"=>"yum", "checksum_type"=>"sha256", "docker_upstream_name"=>nil, "mirror_on_sync"=>true, "download_policy"=>"immediate", "verify_ssl_on_sync"=>true, "upstream_username"=>nil, "upstream_password"=>"[FILTERED]", "ostree_upstream_sync_policy"=>nil, "ostree_upstream_sync_depth"=>nil, "container_repository_name"=>nil, "deb_releases"=>nil, "deb_components"=>nil, "deb_architectures"=>nil, "ignore_global_proxy"=>false}}
2018-07-30T15:56:43 ca3eaa32 [app] [I] Current user: [redacted]
2018-07-30T15:56:43 ca3eaa32 [background] [E] undefined method `content_id' for nil:NilClass
 | Did you mean?  contains_erb? (NoMethodError)
 | /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.6.0/app/lib/actions/katello/repository/update.rb:14:in `plan'

( I’ll try to upload a longer version of this dump later, not allowed now)

I get the same error when I try to remove a product using the foreman GUI :frowning:

undefined method `content_id' for nil:NilClass
Did you mean?  contains_erb?

a small part of production.log here (containing the same error):

 | /opt/theforeman/tfm-ror51/root/usr/share/gems/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `call'
 | /usr/share/passenger/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
 | /usr/share/passenger/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
 | /usr/share/passenger/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
 | /usr/share/passenger/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/logging-2.2.2/lib/logging/diagnostic_context.rb:474:in `block in create_with_logging_context'
2018-07-31T14:21:46 72242ca6 [app] [E] NoMethodError: undefined method `content_id' for nil:NilClass
 | Did you mean?  contains_erb?
 | /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.6.0/app/lib/actions/katello/product/content_destroy.rb:13:in `block in plan'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/dynflow-0.8.37/lib/dynflow/execution_plan.rb:368:in `switch_flow'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/dynflow-0.8.37/lib/dynflow/action.rb:394:in `sequence'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/katello-3.6.0/app/lib/actions/katello/product/content_destroy.rb:7:in `plan'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/dynflow-0.8.37/lib/dynflow/action.rb:486:in `block (3 levels) in execute_plan'
 | /opt/theforeman/tfm/root/usr/share/gems/gems/dynflow-0.8.37/lib/dynflow/middleware/stack.rb:26:in `pass'
 |

I’m having the exact same problem. Foreman 1.9.0.