Ruby on rails Rails中AMP表单的标题设置
Rails中的My AMP表单在终端中返回此错误:Ruby on rails Rails中AMP表单的标题设置,ruby-on-rails,ruby,amp-html,Ruby On Rails,Ruby,Amp Html,Rails中的My AMP表单在终端中返回此错误: 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"] req
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: []):
在浏览器中:
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
但是我确实遵循了官方文档提供的CORS示例代码,根据AMP验证,一切正常
这是我的控制器:
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
你能帮我做这个吗?我可能是头球出了问题。非常感谢
我认为这里的主要问题不是关于未知的格式,而是关于
模板
在您显示的错误中,很明显您在请求中发送了正确的内容类型
request.formats: ["application/json"]
您已经在控制器操作中指定了format.json
所以我想到的唯一一件事就是你的请求的响应有问题。现在再次查看错误消息,它表示您缺少一个模板
ActionController::未知信息格式文字ControllerCreate为
缺少此请求格式和变体的模板
原因是您没有从控制器操作返回任何json响应
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
}
您需要直接从控制器操作呈现json,或者如果不这样做,则需要呈现json
Rails尝试为控制器找到合适的模板
行动
我认为这是这个错误的主要原因
解决方案:
一,。将呈现语句添加到控制器操作中
在这种情况下,您可以执行以下操作:
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
render json: {}, status: :ok
}
二,。创建适当的视图文件,以便Rails可以找到它
Rails将尝试在controller name的目录名中查找名为controller action的视图文件。因此,在您的情况下,由于控制器的名称是题词controller,因此您的文件应该是
app/views/dentitles/create.json
建议
在您提供的链接中,已说明
对于来自允许来源的请求,我们的响应将包含
以下标题:
访问控制允许来源:
AMP访问控制允许来源:
访问控制公开标头:AMP访问控制允许源源源
但您并没有在create操作中设置,为了让客户端读取请求中的其他响应头,应该设置create操作
希望能有所帮助。您的路线对于create来说是什么样的?尝试在请求末尾添加.json,如下所示:http://localhost:3000/inscriptions.json?someparamOr 请确保您的默认格式设置为json,这很有效,非常感谢!我不再面临ActionController::UnknownFormat错误。但在生产上,,当我去www.example.com/?format=amp时,一切都很好,但是当我点击谷歌提供的链接时,当我试图提交我的表单时,我遇到了这个错误:HTTP Origin headerhttps://www-example-com.cdn.ampproject.org 与request.base\u url不匹配https://www.example.com 即使我更新了允许的来源。有什么想法吗?