I am confused about the main difference(s) among link_to
, redirect_to
and render
in Rails. anyone can please explain.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With