重定向到网页的 AMP 版本 (Rails)

Redirecting to AMP Version of a webpage (Rails)

我在项目中使用 Google 的 AMP,并希望在用户创建新资源时重定向到资源的 AMP 版本。

我有一个名为 Guide 的模型。我的控制器 create 操作看起来像这样 - 非常基本:

  def create
    console
@guide = Guide.new(guide_params)

    if @guide.save
    redirect_to @guide
else
    redirect_to :action => "new", notice: "Something went wrong, sorry!"
end 

结束

如何 redirect_to 转到该页面的 AMP 版本?例如它不会转到 /guides/12345,而是转到 /guides/12345.amp

要显示资源的 AMP 版本,您应该将 format: :amp 选项添加到资源的 url。

redirect_to guide_path(@guide, format: :amp)

对于完整的实施,您应该考虑添加 gem,例如 rails_amp