I am confused about the main difference(s) among link_to
, redirect_to
and render
in Rails. anyone can please explain.
A link_to creates a hyperlink to a specific URL, which appears on the HTML.
A redirect_to will decide where to link you to, depending on certain options. For example, if someone is logged on as a user, you might want to show him his settings page, else redirect_to the home page.
A render will open the rendered file, take its content and paste it into the existing file, before sending the whole chunk to the recipient.
Hope I am right.
link_to
is for use in ERB templates. It outputs a link to a specific path or url.
redirect_to
is for use in controllers. It causes the client to request the specified path or url once the controller method exits.
render
is also for use in controllers. It causes Rails to render the specified template.
redirect_to
and render
may only be called once in a given controller method.
link_to will output a standard html anchor=a link (link_to documentation)
redirect_to is commonly used for page responses such as update and delete. It will take the parameters you give it and will direct your page appropriately.
(redirect_to documentation)
render is used for loading partials or loading specific .erb files into others. (render documentation)
There are a bunch of examples on this rails guide which should explain render and redirect_to. link_to is pretty different from rendering and redirect_to