I have a link that passes a parameter to the show action of my post:
<%= link_to post_show_path( user.id, ref: user.ref ) do %>
go to post
<% end %>
But this creates the URL: localhost:3000/post?ref=1445461
Obviously, this will create duplications which kills my SEO. I want to pass that reference to the controller but not show it on the URL. I tried using method post without success:
<%= link_to post_show_path( user.id), ref: user.ref, method: :post do %>
go to post
<% end %>
It doesn’t pass params[:ref]
So first, am I doing it wrong? Is my syntax wrong? and if not, do I have other alternatives? if yes, could you please write it down for me?
I was thinking of doing a 301 redirect, but putting a redirect to the same show action inside the show action obviously creates a loop. I prefer not to pass through another action just to get to the “show” action.
So what do I do? 🙂
2
Answers
By default,
html
link elements do notpost
. If you need to have a link to be able to mimic thepost
request, then you can usebutton_to
helper provided byrails
. Even thoughbutton_to
generates a button tag, you could use some css styling and make it look like a link.Refer to UrlHelper#button_to for more info.
Just add the
ref
param into the path:But if your only concern is SEO duplication, you can as well stick with a GET request and use the
rel=nofollow
directive:Update: if what you need is having multiple urls pointing to the same page (from the SEO point of view), then you may use the canonical URL, possibly also with a 301 redirect, as described e.g. here or here. Google (and others) will then index this page as a single page although multiple urls would point to it.