Ruby on Rails

Ruby on Rails Render Template & Pass Data to View

Mvc | Ruby | Ruby On Rails

Learn Ruby on Rails


This entry is part 3 of 4 in the series Learning Ruby on Rails From Scretch

Ruby on Rails render template: Controller makes a very important decision of which view to load. We have seen in our previous tutorial about how controller loads view based on the url. For example if the url has demo/index. Then Demo Controller’s index method called. And Controller by default loads demo/index.html.erb view from views directory. If you haven’t read our previous tutorial, I highly recommend that you do.

Ruby on Rails Render Template

Ruby on Rails Render Template: In order to render different view from controller, we have to override the default behaviour of controller to load view. We can use the following line of code in our demo controller to load different view. Open demo_controller. rib file and modify its index method like below.

Make sure your rails server is running. Open the browser and navigate to http://localhost:3000/demo/index. You will see hello view is being loaded. Another alternate method for the same thing is now available since ruby on rails 3. Modify above code like:

Ruby on Rails Redirecting Actions

The controller can also able to redirect to some other action. For example Let’s say user requests a page. The request comes to the controller. The controller checks if user logged in then we’ll let them view the page. But if they are not logged in then redirect to any other section of our code. The redirect is a way to indicate browser to the new location to send request. And a new request is being placed in the browser for the new url sent by the server. To Redirect user when the user tries to access hello method of demo controller.

We put a redirect to index method when a user requests hello method. If you navigate to http://localhost:3000/demo/hello. Then it will immediately redirect to http://localhost:3000/demo/index. Try it yourself.

Ruby on Rails View Template

We will create a view file inside views directory and name format will be <filename>.html.erb. Let’s edit our hello.html.erb file.

2nd line uses ruby <%= %> tag to execute 10+12 and output is passed to html. = is used just after opening <% to indicate output. While the 4th line only executes ruby code but no output will be generated. And the 5th line uses x variable to output with string. # {variable} is used in string for variable. And then loop is being executed with multiple ruby code blocks. 2nd block generates output while the rest is only executed ruby code. Open the browser and navigate to http://localhost:3000/demo/index and check result yourself.

Ruby on Rails Pass Data to View

In order to pass data to View, we have to create instance variable in the controller. Open demo controller and modify code as follows:

We just created an instance variable named array in demo controller’s index method. Now we can use it to view. Open hello.html.erb view and modify the code as below.

Instead of the static loop now we have changed loop into dynamic which cycle through a loop and passes 1 by 1 element of the array into d. When all elements are used loop will be finished automatically. Now refresh Browser and check the result.

ruby on rails render template

Hope you like it. For more updates please visit our FB Page.




Series Navigation<< Installing Ruby on Rails on Mac OSX PlatformRuby on Rails Generate Controller & View via rails command >>

Bookmark Link using: bookmark at folkd