what is the difference between link_to, redirect_to, and render?

Sami picture Sami · Jun 21, 2013 · Viewed 23.1k times · Source

I am confused about the main difference(s) among link_to, redirect_to and render in Rails. anyone can please explain.

Answer

RedXVII picture RedXVII · Jun 21, 2013

link_to is used in your view, and generates html code for a link

<%= link_to "Google", "http://google.com" %>

This will generate in your view the following html

<a href="http://google.com">Google</a>

redirect_to and render are used in your controller to reply to a request. redirect_to will simply redirect the request to a new URL, if in your controller you add

redirect_to "http://google.com"

anyone accessing your page will effectively be redirected to Google

render can be used in many ways, but it's mainly used to render your html views.

render "article/show"

This will render the view "app/views/article/show.html.erb"

The following link will explain the redirect_to and the render methods more in detail http://guides.rubyonrails.org/layouts_and_rendering.html