what is the difference between link_to, redirect_to, and render?
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
Difference between $this- render and $this- redirect Symfony2
Redirect()
Redirect performs a 301 or 302 redirect to the specified route/location. You can use this to pass in a full URL I believe. Using this method will cause the URL to change in the address bar.
Because Redirect uses a simple 301/302 header to do the redirect there is no way to pass template parameters to the new location except for on the URL as you would do to any controller or URL.
Render()
Render just renders up the template file you tell it to as a response to the current request. With this you can pass in your array of template parameters as normal.
Forward()
There is also Forward which will forward the request to another controller internally sending that controllers response back as the response to the current request without any redirects. Using this method re-routes the request internally without changing the URL in the address bar.
The key difference here between Render and Redirect is that Render is part of the View system and therefore can pass parameters to the tempaltes. Redirect is part of the Controller system and doesn't know anything about the View. You can pass parameters to the route or URL you are redirecting to but the target location must then do something with them to pass them on to the View.
Difference between render_template and redirect?
redirect
returns a 302 header to the browser, with its Location
header as the URL for the index
function. render_template
returns a 200, with the index.html template returned as the content at that URL.
Iron-Router, what's the difference between redirect and render?
The basic difference is that render
will simply render the passed in template for whatever route the user landed on when the before hook was called, and redirect
will change the route and render the template associated with the new route.
You can use either...just depends on what you want to accomplish. Sometimes you might want to allow the user to sign in from any page and then stay on that page...so, you would use a render
. Other times, you want to send them to a sign-in route and then direct them to specific route following a successful sign-in...in that case, you'd perform a redirect
.
Clear as mud?
What is the difference between $this- render() and $this- redirect()
It looks like they do quite different things:
->redirect($url, ...)
redirect does a HTTP page redirect. Does not directly render a page.->render($view, ...)
render outputs the named view. Does not terminate the current PHP request.
What's the difference between redirect and render in CakePHP?
The call to redirect() issues a HTTP redirect. Nothing happens after the redirect because CakePHP simply stops. Anything you put after the redirect call will not be executed. Instead, the browser simply issues a new HTTP GET to the URL you are redirecting to.
The call to render() simply loads a view. It takes a path to a view, not an URL. It does not redirect. Assume that $userID is '101' in your case. The call to render() would try to load the following file:
app/views/forms/homepage/101.ctp
Since that file does not exist, nothing happens.
What is the proper usage of res.render() and res.redirect() in Express?
look at connect-flash to use rails style flashing of messages.
res.render() will render the view with data passed to it, res.redirect() will redirect a user to another page (at which point the request starts over)
Difference between redirect and view rendering in Spring MVC
First one returns the view while the later redirects to another controller request mapped action. Lets have alook from the code itself.
@RequestMapping(method = RequestMethod.POST)
public String add(@Valid AppointmentForm appointment, BindingResult result) {
if (result.hasErrors()) {
return "appointments/new";
}
Here when the result have errors, it renders the view appointments/new
so that the user can enter the correct details and add the apppointment again. URL will not change in the browser
appointmentBook.addAppointment(appointment);
return "redirect:/appointments";
}
But here, when the results has no error, this controller action redirect the website to the URL /appointments. Check the web browser URL which is changed to the redirected URL
Regarding forward:
vs redirect:
Quoted from this Satckoverflow answer Why do we use redirect in Spring MVC
Using the redirect prefix in your controller will generate a HTTP response with the 302 status code and the location header pointing to the redirection URL. The browser will then redirect to that URL (model exposed in the first request will be lost, and the browser URL will be the second one).
Using the forward prefix, the forward will be done internally by the servlet, so there's no need of a second request (URL will remain the same). The forward prefix should only be used in requests that can be safely repeated by the browser. That's not the case when you send a form which changes the database state (reloading the browser will lead to duplicate submissions). In these cases you should use redirect and apply the POST-redirect-GET pattern.
Are redirect_to and render exchangeable?
If you're using render
, when the user refreshes the page, it will submit the previous POST request again. This may cause undesired results like duplicate purchase and others.
But if you're using redirect_to
, when the user refreshes the page, it will just request that same page again. This is also known as the Post/Redirect/Get (PRG) pattern.
So the place where redirect_to
should be used is when you're doing a HTTP POST request and you don't want the user to resubmit the request when it's done (which may cause duplicate items and other problems).
In Rails, when a model fails to be saved, render
is used to redisplay the form with the same entries that was filled previously. This is simpler because if you use redirect, you'll have to pass the form entries either using parameters or session. The side effect is that if you refresh the browser, it will try to resubmit the previous form entries. This is acceptable because it will probably fail the same way, or if it's successful now, it was what the user should expect in the first place anyway.
For more in depth explanation about render
and redirect
, you should read this article.
Related Topics
Is There Equivalent for PHP's Print_R in Ruby/Rails
How to Combine Overlapping Time Ranges (Time Ranges Union)
Devise - How to Change Setting So That Email Addresses Don't Need to Be Unique
Gem Install Error (Sass Compass)
Dynamically Load Google Maps Markers with Gmaps4Rails
Database Configuration Does Not Specify Adapter
"Whenever" Gem Running Cron Jobs on Heroku
How to Change All the Keys of a Hash by a New Set of Given Keys
Redefining a Single Ruby Method on a Single Instance with a Lambda
Ruby: Why Does Puts Call To_Ary
Why Does Ruby /[[:Punct:]]/ Miss Some Punctuation Characters
Missing Symbol When Installing Ruby-2.3.0 on Os X 10.11.6 by Rvm
Why Isn't 'Method=' Treated the Same as Any Other Method