InterviewSolution
| 1. |
Render Vs. Redirect_to In Ruby On Rails ? |
|
Answer» render will render a particular view USING the instance variables available in the ACTION. For example if a render was used for the new action, when a USER goes to /new, the new action in the controller is called, instance variables are created and then passed to the new view. Rails creates the html for that view and returns it back to the user's browser. This is what you WOULD consider a normal page load. redirect_to will send a redirect to the user’s browser telling it to re-request a new URL as 302 redirect response. Then the browser will send a new request to that URL and it will go through the action for that URL, oblivious to the fact that it was redirected to. None of the variables created in the action that caused the redirect will be available to the redirected view. This is what happens when you CLICK on ‘Create’ in a form and the object is created and you’re redirected to the edit view for that object. render will render a particular view using the instance variables available in the action. For example if a render was used for the new action, when a user goes to /new, the new action in the controller is called, instance variables are created and then passed to the new view. Rails creates the html for that view and returns it back to the user's browser. This is what you would consider a normal page load. redirect_to will send a redirect to the user’s browser telling it to re-request a new URL as 302 redirect response. Then the browser will send a new request to that URL and it will go through the action for that URL, oblivious to the fact that it was redirected to. None of the variables created in the action that caused the redirect will be available to the redirected view. This is what happens when you click on ‘Create’ in a form and the object is created and you’re redirected to the edit view for that object. |
|