Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 718
Sami Avatar asked Jun 21 '13 13:06

Sami


People also ask

What is render in Ruby?

Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code. A view is what the user sees.

How do I redirect in Rails?

Rails's redirect_to takes two parameters, option and response_status (optional). It redirects the browser to the target specified in options. This parameter can be: Hash - The URL will be generated by calling url_for with the options.


1 Answers

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

like image 97
RedXVII Avatar answered Sep 23 '22 00:09

RedXVII