how to use “render” under controller

Under the MVC framwork, Controller is in charge of handling requests between Model and View. With this function in mind, it’s possible for Controller to tell view to render different things. Below are some examples:

  • render :text

Simply rendering a string with render "This is a string", which can be useful if the codes get too complex.

  • render :new

This would tell Rails to look for and render a page called new.html.erb under the Views folder.

  • render :layout

The default layout under Rails is app/view/layouts/application.html.erb, but sometimes we may want the page to render a different layout, say with a color differen color scheme to distinguish an admin interface. In this case, we could build a new layout under app/view/layouts/admin.html.erb, and under the Controller file add in a line of code: layout "admin".