What is the difference between render and redirect? - Lesson learned 2

-Redirect is a method that is used to issue the error message in case the page is not found or it issues a 302 to the browser. Whereas, render is a method used to create the content.

-Redirect is used to tell the browser to issue a new request. Whereas, render only works in case the controller is being set up properly with the variables that needs to be rendered.

-Redirect is used when the user needs to redirect its response to some other page or URL. Whereas, render method renders a page and generate a code of 200.

-Redirect is used as:
redirect_to: controller => ‘users’, :action => ‘new’

-Render is used as:
render: partial
render: new -> this will call the template named as new.rhtml without the need of redirecting it to the new action.

 

Why did all this started?

I noticed that following code using flash.now was not working. However, as per API documentation, it should have worked fine.

      flash.now[:warning] = "User cannot be deleted."
      redirect_to admin_users_path

However, following code without using .now did work.

      flash[:warning] = "User cannot be deleted."
      redirect_to admin_users_path

 

The reason is as follows based on ruby guide information:

When redirecting use

flash[:warning] = "flash will make the message available to the next request"

When rendering use

flash.now[:warning] = "flash will make the message available to the same request"

 

 

Rating: