0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 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.