skip to Main Content

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


  1. By default, html link elements do not post. If you need to have a link to be able to mimic the post request, then you can use button_to helper provided by rails. Even though button_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.

    Login or Signup to reply.
  2. Just add the ref param into the path:

    <%= link_to "go to post", post_show_path(user.id, ref: user.ref), method: :post %>
    

    But if your only concern is SEO duplication, you can as well stick with a GET request and use the rel=nofollow directive:

    <%= link_to "go to post", post_show_path(user.id, ref: user.ref), rel: "nofollow" %>
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search