0
0
Ruby on Railsframework~20 mins

Link and URL helpers in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Link & URL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Rails link_to helper?
Consider the following code in a Rails view:
link_to 'Profile', user_path(42), class: 'btn'

What HTML does this generate?
Ruby on Rails
link_to 'Profile', user_path(42), class: 'btn'
A<button class="btn">Profile</button>
B<a href="/users/42" class="btn">Profile</a>
C<a href="/profile/42" class="btn">Profile</a>
D<a class="btn">Profile</a>
Attempts:
2 left
💡 Hint
Remember that user_path(42) generates the URL for user with id 42.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses link_to with a block?
Which of the following is the correct syntax to use link_to with a block in Rails?
A
link_to(user_path(5)) do
  'Click here'
end
Blink_to(user_path(5)) { 'Click here' }
Clink_to do user_path(5) 'Click here' end
Dlink_to(user_path(5)) do 'Click here' end
Attempts:
2 left
💡 Hint
Blocks in Ruby can be written with do...end or curly braces, but link_to with block usually uses do...end with proper indentation.
🔧 Debug
advanced
2:00remaining
Why does this link_to raise an error?
Given this code:
link_to 'Edit', edit_user_path

Why does it raise an error?
Ruby on Rails
link_to 'Edit', edit_user_path
Aedit_user_path is not a valid Rails helper
Blink_to requires a block when using edit_user_path
Cedit_user_path requires an argument for the user id, but none was given
Dlink_to cannot use path helpers as the second argument
Attempts:
2 left
💡 Hint
Check the method signature of edit_user_path in Rails routes.
state_output
advanced
2:00remaining
What is the value of the generated URL?
Given this route:
resources :articles

and this code:
article_url(10, host: 'example.com')

What is the full URL generated?
Ruby on Rails
article_url(10, host: 'example.com')
Ahttp://example.com/articles/10
Bhttps://example.com/articles/10
C/articles/10
Dhttp://localhost/articles/10
Attempts:
2 left
💡 Hint
article_url generates a full URL including protocol and host.
🧠 Conceptual
expert
3:00remaining
Which option correctly explains polymorphic_url usage?
In Rails, what does polymorphic_url(@comment) do if @comment belongs to a Post?
AGenerates the URL for the comment nested under its post, like /posts/:post_id/comments/:id
BGenerates the URL for the post only, ignoring the comment
CRaises an error because polymorphic_url cannot handle nested resources
DGenerates the URL for the comment only, ignoring its parent post
Attempts:
2 left
💡 Hint
Polymorphic helpers build URLs based on the object and its associations.