Introduction

I assume you have already read my previous article on Using ERB tags In Ruby on Rails templates. Now, you will learn about two ways of page linking methods: target linking and adding URL parameters page links in Ruby on Rails templates.

Requirements

How to create links in Rails
Step 1
  • First create rails application and generate controller view ,if you dont know how to create application creation you must read my previous article create application in ruby on rails,add routing code in route.rb, controller configuration and Rails server is active.(or) If you have Rails application use that previous application.
  • After completing the application creation we need two HTML templates files in this article that files named as main.html.erb and index.html.erb.

Main.html.erb code

  1. <h1>Home#main</h1>
  2. <h2> This is Main Page</h2>
  3. <p>Find this file in app/views/home/main.html.erb</p>

  • The index page was already created in controller creation so we don't create that we should use that file stored in app>> views>>home.

Index.html.erb code

  1. <h1>Home#index</h1>
  2. <h2> This is Index Page</h2>
  3. <p>Find this file in app/views/home/index.html.erb</p>



Now we create links in our HTML templates when the user can click below the link that will initiate a new request.

HTML Link Tag

  • This is a basic HTML link, the text that we want to display for the link is inside the <a>tags.

    <a href="/home/index">Index Page</a>

Rails ERB Tag

<%= link_to(text, target) %>

Link Target

“home/index”

Step 2

Now we use normal HTML Link method in application.open index.html.erb file in app>>views>>home folder use in atom editor.



After you open index file now add following HTML link code in that file and end with <br /> tag.

  1. <a href="main">Home Page 1</a><br/>



It is a just html link tag only, Nothing ERB tag so its work fine in Rails application.

Step 3

Now check in the browser by visiting Localhost:3000/home/index.



Above the output we can see the normal HTML link tag in the index page, if you like moving to the main.html.erb page, simply click that HTML link and it will go in the main page in browser. The result looks like below.

Step 4 ERB link tag in rails application.

  1. <%= link_to(‘Home page’, {:action => ‘home’ }) %><br />
  • Now check in the browser by visiting Localhost:3000/home/index.

Step 5

Above the page linking tags are used in the URL in Rails.These URL links are most used in action and controllers, Now we will see one example.

Page link with parameters

Example

/home/main/1?page=2&name=raj

Step 6

After visiting result you can see parameter link main.html.erb page, when you click that link the controller action goes to index.html.erb file page.



Above the result you can see additional parameters in address bar. We successfully added parameters in URL.

Conclusion

I hope you understood Page linking with two Page linking tags and how to add URL parameters in Page links in Ruby on Rails application files. In future articles we will learn more about Ruby on Rails step by step. Keep in touch.