The link_to helper creates an HTML anchor (<a>) tag that links to a specified URL or path. It helps generate clickable links easily in views.
Click to reveal answer
beginner
How do you generate a URL for a named route in Rails?
You use URL helpers that Rails creates from your routes, like posts_path or post_url(@post). These helpers return the path or full URL for the route.
Click to reveal answer
intermediate
What is the difference between _path and _url helpers in Rails?
_path helpers return a relative path (like /posts/1), while _url helpers return the full URL including the domain (like http://example.com/posts/1).
Click to reveal answer
intermediate
How can you add HTML options like CSS classes or data attributes to a link_to helper?
You pass a hash of HTML options as the last argument to <code>link_to</code>. For example, <code>link_to 'Home', root_path, class: 'nav-link', data: { toggle: 'dropdown' }</code> adds a class and data attribute.
Click to reveal answer
intermediate
What helper would you use to create a link that submits a DELETE request in Rails?
Use link_to with the method: :delete option. Rails uses JavaScript to convert the link click into a DELETE HTTP request.
Click to reveal answer
Which helper generates a clickable link in Rails?
Alink_to
Burl_for
Cform_for
Dbutton_to
✗ Incorrect
link_to creates clickable links. url_for generates URLs but not links.
What does posts_path return?
AFull URL including domain
BJavaScript code
CRelative path like '/posts'
DHTML anchor tag
✗ Incorrect
posts_path returns the relative path for posts.
How do you add a CSS class to a link_to helper?
AUse <code>class</code> key in options hash
BPass class name as second argument
CWrap link_to in a <code><div></code>
DAdd class in controller
✗ Incorrect
You add HTML options like class using a hash as the last argument.
Which option makes link_to send a DELETE request?
Amethod: :get
Bmethod: :delete
Cmethod: :post
Dmethod: :put
✗ Incorrect
method: :delete tells Rails to send a DELETE HTTP request.
What is the difference between post_path(@post) and post_url(@post)?
ANo difference
B<code>post_path</code> returns full URL, <code>post_url</code> returns path
COne is for GET, other for POST
D<code>post_path</code> returns path, <code>post_url</code> returns full URL
✗ Incorrect
post_path returns relative path; post_url returns full URL with domain.
Explain how to create a link in Rails that looks like a button and sends a DELETE request when clicked.
Think about link_to options and styling.
You got /4 concepts.
Describe the difference between URL helpers ending with _path and those ending with _url in Rails.
Consider what the browser needs to navigate.
You got /4 concepts.
Practice
(1/5)
1. What does the Rails helper link_to primarily do?
easy
A. Generates a plain URL string
B. Creates a clickable HTML link
C. Fetches data from the database
D. Starts a new Rails server
Solution
Step 1: Understand the purpose of link_to
The link_to helper is designed to create clickable links in HTML pages.
Step 2: Differentiate from url_for
Unlike url_for, which returns a URL string, link_to outputs an anchor tag with the link.
Final Answer:
Creates a clickable HTML link -> Option B
Quick Check:
link_to = clickable link [OK]
Hint: Remember: link_to makes links, url_for makes URLs [OK]
Common Mistakes:
Confusing link_to with url_for
Thinking link_to fetches data
Assuming link_to starts server
2. Which of the following is the correct syntax to create a link to the home page with text 'Home' using link_to?
easy
A. link_to 'Home', root_path
B. link_to root_path, 'Home'
C. link_to 'Home' => root_path
D. link_to('Home' root_path)
Solution
Step 1: Recall link_to syntax
The correct syntax is link_to 'Link Text', path_or_url.
Step 2: Match options to syntax
link_to 'Home', root_path matches this pattern exactly. Others have wrong order or syntax errors.
The link_to helper creates an anchor tag with href set to the URL from user_path(5).
Step 2: Check URL and class attribute
user_path(5) generates "/users/5". The class attribute is added as class="btn".
Final Answer:
<a href="/users/5" class="btn">Profile</a> -> Option C
Quick Check:
link_to with path and class outputs anchor with href and class [OK]
Hint: link_to outputs <a> with href from path helper [OK]
Common Mistakes:
Forgetting href attribute
Confusing button tag with link
Wrong URL path format
4. Identify the error in this code snippet:
link_to 'Edit' edit_user_path user.id
medium
A. Missing parentheses around method argument
B. Incorrect method name, should be edit_user_url
C. link_to requires a block, not arguments
D. Missing comma between arguments
Solution
Step 1: Analyze link_to syntax
The link_to helper requires comma-separated arguments for text and path.
Step 2: Check argument separation
There is no comma between 'Edit' and edit_user_path user.id, causing a syntax error.
Final Answer:
Missing comma between arguments -> Option D
Quick Check:
Arguments must be comma-separated [OK]
Hint: Always separate arguments with commas in helpers [OK]
Common Mistakes:
Omitting commas between arguments
Confusing _path and _url helpers
Thinking link_to needs a block always
5. You want to create a link that opens in a new browser tab with the text 'Docs' linking to https://guides.rubyonrails.org. Which is the correct link_to usage?
hard
A. link_to 'Docs', 'https://guides.rubyonrails.org', target: '_blank', rel: 'noopener'
B. link_to 'Docs', 'https://guides.rubyonrails.org', new_tab: true
C. link_to 'Docs', url_for('https://guides.rubyonrails.org'), open_in_new: true
D. link_to 'Docs', 'https://guides.rubyonrails.org', open: '_new'
Solution
Step 1: Identify how to open link in new tab
In HTML, target="_blank" opens a link in a new tab.
Step 2: Add security attribute
Adding rel="noopener" prevents security risks when opening new tabs.
Step 3: Check options syntax
link_to 'Docs', 'https://guides.rubyonrails.org', target: '_blank', rel: 'noopener' correctly uses target: '_blank' and rel: 'noopener' as options in link_to.
Final Answer:
link_to 'Docs', 'https://guides.rubyonrails.org', target: '_blank', rel: 'noopener' -> Option A
Quick Check:
Use target: '_blank' and rel: 'noopener' for new tab links [OK]
Hint: Use target: '_blank' and rel: 'noopener' for safe new tabs [OK]
Common Mistakes:
Using non-standard options like new_tab or open_in_new
Forgetting rel: 'noopener' for security
Using incorrect target values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.