My AMP form in Rails is returning this error in the terminal :
Completed 406 Not Acceptable in 338ms (ActiveRecord: 54.1ms)
ActionController::UnknownFormat (InscriptionsController#create is missing a template for this request format and variant.
request.formats: ["application/json"]
request.variant: []):
And in the browser :
POST http://localhost:3000/inscriptions?__amp_source_origin=http%3A%2F%2Flocalhost%3A3000 406 (Not Acceptable)
Response must contain the AMP-Access-Control-Allow-Source-Origin header
Form submission failed: Error: Response must contain the AMP-Access-Control-Allow-Source-Origin header
at bb.f.assert (https://cdn.ampproject.org/v0.js:21:319)
at y (https://cdn.ampproject.org/v0.js:26:213)
at https://cdn.ampproject.org/v0.js:179:339
However I did follow the CORS sample code provided by the official documentation https://amp.dev/documentation/guides-and-tutorials/learn/amp-caches-and-cors/amp-cors-requests?referrer=ampproject.org, and according to the AMP validation, everything is fine,
This is my controller :
def create
@inscription = Inscription.new(inscription_params)
@inscription.save
subscriber_email = inscription_params[:email].downcase
if Subscriber.where(email: subscriber_email).count == 0
@subscriber = Subscriber.new
@subscriber.email = subscriber_email
@subscriber.save
@new_subscriber = true
else
@subscriber = Subscriber.where(email: subscriber_email).first
@new_subscriber = false
end
[...]
respond_to do |format|
format.html { redirect_to root_path, notice: "Merci ! Nous avons bien reçu votre inscription !" }
format.js
format.json {
allowed_origins = ["https://example.com", "https://example.com.cdn.ampproject.org/", "http://example.com.amp.cloudflare.com", "https://cdn.ampproject.org"]
allowed_source_origin = "https://example.com"
source_origin = params[:__amp_source_origin]
origin = request.headers["Origin"]
response.set_header('Content-type', 'application/json')
response.set_header('Access-Control-Allow-Credentials', 'true')
response.set_header('Access-Control-Allow-Origin', origin)
response.set_header('AMP-Access-Control-Allow-Source-Origin', source_origin)
p response.headers
}
end
end
May you help me with this? I probably did something wrong with the headers.