Rails helpers: link_to
This course covers link_to, the Rails view helper that builds an <a href> tag from Ruby. It starts with the two required arguments, visible text and then destination, and the plain anchor tag they produce. It then replaces hardcoded paths with named route helpers such as partners_path and dashboard_path, so links follow changes in config/routes.rb. Later lessons add the optional options hash for class: and other HTML attributes. They also cover route helpers that take an argument and calling link_to from bin/rails runner without a server.
What you'll learn
- ✓The two required arguments of link_to and the order they go in
- ✓Using named route helpers like partners_path in place of hardcoded paths
- ✓Adding class:, target:, rel: and other HTML attributes through the options hash
- ✓Passing a route helper that takes an argument, such as public_profile_path(@user.username)
- ✓Calling link_to through ActionController::Base.helpers in bin/rails runner
From <a href> to link_to
Lesson 1 of Links and images, on the <a> tag, showed these two lines in an ERB file without explaining them:
Read the full lesson free →Curriculum
7 lessonsFrom <a href> to link_to
Shows how link_to and a route helper write the same <a href> tag that plain HTML uses.
link_to's two required arguments
Covers the two required arguments of link_to, text then destination, and the plain anchor tag they return.
Swapping the hardcoded string for a route helper
Replaces a hardcoded path with partners_path, so every link follows changes made in config/routes.rb.
The options hash: class: and other HTML attributes
Explains the optional options hash, where class:, target:, rel: and other keys become HTML attributes.
A route helper that takes an argument
Passes public_profile_path(@user.username) as the destination, a route helper that needs an argument first.
Verifying every claim live
Calls link_to through ActionController::Base.helpers in bin/rails runner, with no server or browser involved.
Putting it together: link_to's full shape
Walks through one real view line that combines text, a route helper with an argument, and attributes.