Intern Track Roadmap

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
Free preview · Lesson 1 2 min read

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 lessons
Type to start searching...