Search code examples
ruby-on-railsamp-html

Redirecting to AMP Version of a webpage (Rails)


I'm using Google's AMP in a project, and want to redirect to the AMP version of a resource when a user creates a new resource.

I have a model named Guide. My controller create action, looks like this - pretty basic:

  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 

end

How do I make redirect_to go to the AMP version of that page? e.g. instead of going to /guides/12345, it goes to /guides/12345.amp.


Solution

  • To show the AMP version of the resource you should add the format: :amp option to the url of the resource.

    redirect_to guide_path(@guide, format: :amp)
    

    for a full blown implementation you should consider adding a gem like rails_amp