0
0
Ruby on Railsframework~30 mins

Link and URL helpers in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Link and URL Helpers in Rails
📖 Scenario: You are building a simple Rails web page for a bookstore. You want to add links to navigate between the home page, the books list, and a contact page.
🎯 Goal: Build a Rails view file that uses link and URL helpers to create navigation links for Home, Books, and Contact pages.
📋 What You'll Learn
Create a variable with the page titles and their paths
Add a helper variable for the base URL
Use link_to helper to create links for each page
Use url_for helper to generate a URL for the contact page
💡 Why This Matters
🌍 Real World
Web developers often need to create navigation menus and links dynamically in Rails applications using link and URL helpers.
💼 Career
Understanding Rails link and URL helpers is essential for building user-friendly navigation and routing in real-world Rails web projects.
Progress0 / 4 steps
1
Create a hash with page names and paths
Create a Ruby hash called pages with these exact entries: 'Home' => root_path, 'Books' => books_path, and 'Contact' => contact_path.
Ruby on Rails
Need a hint?

Use a Ruby hash with keys as page names and values as path helpers.

2
Add a base URL variable
Add a variable called base_url and set it to root_url.
Ruby on Rails
Need a hint?

Use the root_url helper to get the full base URL.

3
Create links using link_to
Use link_to helper inside a loop to create links for each page in pages. Use name and path as loop variables.
Ruby on Rails
Need a hint?

Use pages.each do |name, path| and inside the block call link_to name, path.

4
Generate a URL for Contact page using url_for
Use url_for helper to generate the full URL for the contact page and assign it to a variable called contact_full_url.
Ruby on Rails
Need a hint?

Use url_for(controller: 'pages', action: 'contact', only_path: false) to get the full URL.