- Create Ruby on Rails Project from Scratch, a beginner’s guide
- Installing Ruby on Rails on Mac OSX Platform
- Ruby on Rails Render Template & Pass Data to View
- Ruby on Rails Generate Controller & View via rails command
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.
1 2 3 4 5 6 7 8 9 10 11 |
class DemoController < ApplicationController def index # Ruby on Rails Render Template render(:template => "demo/hello") end def hello end end |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
class DemoController < ApplicationController def index # Ruby on Rails Render Template render("demo/hello") end def hello end end |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class DemoController < ApplicationController def index # Ruby on Rails Render Template render("demo/hello") end def hello #Ruby on Rails Redirect redirect_to(:controller => "demo", :action => "index") end end |
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.
1 2 3 4 5 6 7 8 9 |
<h1>Hello View</h1> <%= 10 + 12 %> <% x = "Test" %> <%= "Hello #{x}" %> <% 5.times do %> test: <%= "hello" %> <% end %> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class DemoController < ApplicationController def index @array = [1,2,3,4,5] # Ruby on Rails Render Template render("demo/hello") end def hello redirect_to(:controller => "demo", :action => "index") end end |
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.
1 2 3 4 5 6 7 8 9 |
<h1>Hello View</h1> <%= 10 + 12 %> <% x = "Test" %> <%= "Hello #{x}" %> <hr/> <% @array.each do |d| %> <%=d%><br/> <% end %> |
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.
Hope you like it. For more updates please visit our FB Page.