Bird
Raised Fist0
Ruby on Railsframework~20 mins

Named routes and path helpers in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Named Routes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
1:30remaining
What is the output of this Rails view helper call?
Given the following route in config/routes.rb:
resources :articles

What does article_path(5) return?
A"/articles/5"
B"/articles/show/5"
C"/article/5"
D"/articles?id=5"
Attempts:
2 left
💡 Hint
Think about the RESTful route generated by resources :articles for the show action.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a named route with a custom path helper?
You want to create a route for /profile that maps to users#show and use the helper profile_path. Which route definition is correct?
Aget '/profile', to: 'users#show', as: :profile
Bget '/profile', controller: 'users', action: 'show', helper: :profile
Cmatch '/profile' => 'users#show', as: 'profile_path'
Dget '/profile', to: 'users#show', name: :profile
Attempts:
2 left
💡 Hint
The as: option defines the named route helper.
❓ state_output
advanced
1:30remaining
What is the value of edit_article_path(@article) if @article.id = 10?
Assuming standard RESTful routes for articles, what path does edit_article_path(@article) generate?
A"/articles/10/edit_article"
B"/articles/edit/10"
C"/articles/10/edit"
D"/edit/articles/10"
Attempts:
2 left
💡 Hint
The edit route for a resource appends /edit after the resource ID.
🔧 Debug
advanced
1:30remaining
Why does this route helper call raise an error?
Given the route resources :comments, what error occurs when calling comment_path without arguments?
Ruby on Rails
comment_path
ANoMethodError: undefined method 'comment_path'
BArgumentError: missing required keys: [:id]
CNameError: uninitialized constant CommentPath
DRuntimeError: route not found
Attempts:
2 left
💡 Hint
Show and other member routes require an ID argument.
🧠 Conceptual
expert
2:00remaining
Which option correctly explains the difference between *_path and *_url helpers?
In Rails, what is the main difference between article_path(1) and article_url(1)?
A<code>article_path(1)</code> is used only in views, <code>article_url(1)</code> only in controllers.
B<code>article_path(1)</code> returns the full URL, and <code>article_url(1)</code> returns only the path.
C<code>article_path(1)</code> generates a GET request, <code>article_url(1)</code> generates a POST request.
D<code>article_path(1)</code> returns a relative path like <code>/articles/1</code>, while <code>article_url(1)</code> returns the full URL including protocol and host, like <code>http://localhost:3000/articles/1</code>.
Attempts:
2 left
💡 Hint
Think about what the browser needs to navigate to a page versus what you might send in an email.

Practice

(1/5)
1. What is the main purpose of named routes in Rails?
easy
A. To automatically generate database tables
B. To style HTML elements with CSS
C. To give easy-to-use names for URLs in the app
D. To handle user authentication

Solution

  1. Step 1: Understand named routes concept

    Named routes assign simple names to URL patterns in Rails routes file.
  2. Step 2: Identify their purpose

    These names help generate URL or path helpers for cleaner, readable code.
  3. Final Answer:

    To give easy-to-use names for URLs in the app -> Option C
  4. Quick Check:

    Named routes = URL names [OK]
Hint: Named routes = easy URL names for helpers [OK]
Common Mistakes:
  • Confusing named routes with database features
  • Thinking named routes style HTML
  • Mixing named routes with authentication
2. Which syntax correctly defines a named route for a 'profile' page in config/routes.rb?
easy
A. get '/profile', to: 'users#show', as: 'profile'
B. route '/profile' => 'users#show', name: 'profile'
C. get '/profile' => 'users#show', named: 'profile'
D. path '/profile', controller: 'users', action: 'show', alias: 'profile'

Solution

  1. Step 1: Recall correct named route syntax

    Rails uses get '/url', to: 'controller#action', as: 'name' format.
  2. Step 2: Match options to syntax

    get '/profile', to: 'users#show', as: 'profile' matches this exactly; others use invalid keywords.
  3. Final Answer:

    get '/profile', to: 'users#show', as: 'profile' -> Option A
  4. Quick Check:

    Correct named route syntax = get '/profile', to: 'users#show', as: 'profile' [OK]
Hint: Use 'get', 'to:', and 'as:' for named routes [OK]
Common Mistakes:
  • Using wrong keywords like 'route' or 'named'
  • Mixing hash rocket and symbol syntax incorrectly
  • Forgetting 'as:' for naming the route
3. Given this route: get '/dashboard', to: 'home#dashboard', as: 'dashboard', what does dashboard_path return?
medium
A. '/dashboard_path'
B. '/home/dashboard'
C. 'dashboard_url'
D. '/dashboard'

Solution

  1. Step 1: Understand named route helper

    The named route 'dashboard' creates dashboard_path helper returning the URL path string.
  2. Step 2: Match helper output

    Since route is '/dashboard', dashboard_path returns '/dashboard'.
  3. Final Answer:

    '/dashboard' -> Option D
  4. Quick Check:

    dashboard_path = '/dashboard' [OK]
Hint: Named route 'foo' creates 'foo_path' returning URL path [OK]
Common Mistakes:
  • Confusing _path with _url helpers
  • Expecting controller/action strings
  • Adding extra suffixes like '_path' in output
4. Identify the error in this route definition: get '/items', to: 'items#index', as: items_path
medium
A. The HTTP verb 'get' is incorrect here
B. The named route should be a symbol or string, not a method call
C. The controller action 'items#index' is invalid syntax
D. The URL path '/items' must not start with a slash

Solution

  1. Step 1: Check named route naming

    The as: option expects a symbol or string name, not a method call like items_path.
  2. Step 2: Identify correct usage

    It should be as: :items or as: 'items' to create items_path helper.
  3. Final Answer:

    The named route should be a symbol or string, not a method call -> Option B
  4. Quick Check:

    Use symbol/string for 'as:' [OK]
Hint: Use symbol/string for 'as:', not helper method call [OK]
Common Mistakes:
  • Passing helper method name instead of symbol/string
  • Misunderstanding 'as:' purpose
  • Thinking HTTP verb or path is wrong here
5. You want to create a named route for editing a user profile with URL /users/:id/edit. Which route definition and helper usage is correct?
hard
A. Route: get '/users/:id/edit', to: 'users#edit', as: 'edit_user'; Helper: edit_user_path(5)
B. Route: get '/users/edit/:id', to: 'users#edit', as: 'edit_user'; Helper: edit_user_path(5)
C. Route: get '/users/:id/edit', to: 'users#edit', as: :edit_user; Helper: edit_user_url
D. Route: get '/users/edit/:id', to: 'users#edit', as: :edit_user; Helper: edit_user_path

Solution

  1. Step 1: Confirm correct URL pattern

    The edit user URL is conventionally '/users/:id/edit'.
  2. Step 2: Check named route and helper usage

    Route: get '/users/:id/edit', to: 'users#edit', as: 'edit_user'; Helper: edit_user_path(5) uses string for 'as:' and calls edit_user_path(5) which is correct. Route: get '/users/:id/edit', to: 'users#edit', as: :edit_user; Helper: edit_user_url uses symbol but calls edit_user_url without argument, which is incomplete.
  3. Final Answer:

    Route: get '/users/:id/edit', to: 'users#edit', as: 'edit_user'; Helper: edit_user_path(5) -> Option A
  4. Quick Check:

    Correct path + named route + helper call = Route: get '/users/:id/edit', to: 'users#edit', as: 'edit_user'; Helper: edit_user_path(5) [OK]
Hint: Use '/users/:id/edit' and call edit_user_path(id) [OK]
Common Mistakes:
  • Swapping :id and 'edit' in URL path
  • Using _url helper without required id argument
  • Using symbol/string inconsistently for 'as:'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard