0
0
Ruby on Railsframework~5 mins

Link and URL helpers in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the link_to helper do in Rails?
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
What does posts_path return?
AFull URL including domain
BJavaScript code
CRelative path like '/posts'
DHTML anchor tag
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>&lt;div&gt;</code>
DAdd class in controller
Which option makes link_to send a DELETE request?
Amethod: :get
Bmethod: :delete
Cmethod: :post
Dmethod: :put
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
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.