How to Use Rails link_to Method (With Examples) - RubyGuides

Ask questions Research chat →

https://www.rubyguides.com/2019/05/rails-link_to-method/ · scraped

rails

Attachments

Scraped Content

— 705 words · 2026-02-14 17:38:41 UTC ·

Excerpt

![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/87bc63f7-8d6b-429a-85be-866390066620/rails-link-to-method.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466RBJTN7EC%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T173840Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJHMEUCIQD%2FpgI5WFHFJvR6nJ5v8lMQDfe915JTGJaWkhzmU%2F6qWAIga39scwyYTWYiTi%2FdXKyJ30Btu3DqLzNxXrh4FBMj9B0q%2FwMIChAAGgw2Mzc0MjMxODM4MDUiDJPIuQOdsisNi4XrTSrcA1H3PF1e5py9iRAxYjfkLd5mAe0pScWXk8WI6%2BtfmYASGIQ3vSKP3nMNotfuUopLL%2B9%2F2eHscWRu2cU8cBsA8ajK%2BHlndSIAWyqe6G%2Bchu8Y8Kt%2BigLiXbXmhtgo7jYT8w2CIwX83efRVxUMkz9fzehRusN8gfFEm8k1XchZAPkiixdi6u8%2FKX0iOBstHV6qtFceC2WsJxR%2B16sjSRJyMIUm%2F0teGaPo4gDvsq4JR5NNkFL8lP3BDk67UQqCpVlv2bnPhTAEA%2BlzAT95BHMufYjOS6H3MxsV0pvDL46BeCTr0qv6HMGX7g2KdSjzl%2FQBaqDSpE7V09AexQw8W4COTwpghWTOELr9BCwIjbUq03SMFrDatyaNSiJe%2FhHQ2M3e8ZJKE8LykTMi%2FsFy%2BOHgX40H
![](https://prod-files-secure.s3.us-west-2.amazonaws.com/871f1661-80b8-4d0c-ac3b-2adfc6ff4c66/87bc63f7-8d6b-429a-85be-866390066620/rails-link-to-method.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAZI2LB466RBJTN7EC%2F20260214%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20260214T173840Z&X-Amz-Expires=3600&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEEEaCXVzLXdlc3QtMiJHMEUCIQD%2FpgI5WFHFJvR6nJ5v8lMQDfe915JTGJaWkhzmU%2F6qWAIga39scwyYTWYiTi%2FdXKyJ30Btu3DqLzNxXrh4FBMj9B0q%2FwMIChAAGgw2Mzc0MjMxODM4MDUiDJPIuQOdsisNi4XrTSrcA1H3PF1e5py9iRAxYjfkLd5mAe0pScWXk8WI6%2BtfmYASGIQ3vSKP3nMNotfuUopLL%2B9%2F2eHscWRu2cU8cBsA8ajK%2BHlndSIAWyqe6G%2Bchu8Y8Kt%2BigLiXbXmhtgo7jYT8w2CIwX83efRVxUMkz9fzehRusN8gfFEm8k1XchZAPkiixdi6u8%2FKX0iOBstHV6qtFceC2WsJxR%2B16sjSRJyMIUm%2F0teGaPo4gDvsq4JR5NNkFL8lP3BDk67UQqCpVlv2bnPhTAEA%2BlzAT95BHMufYjOS6H3MxsV0pvDL46BeCTr0qv6HMGX7g2KdSjzl%2FQBaqDSpE7V09AexQw8W4COTwpghWTOELr9BCwIjbUq03SMFrDatyaNSiJe%2FhHQ2M3e8ZJKE8LykTMi%2FsFy%2BOHgX40HgLL4Pl0wUdKOWkug5Je3qH0PGDHVNr%2B1ac6OcOHOVyj1L%2BOZ4xBLLofujWl8LJ8c7ed%2FSYp1gJYYgharUfeI3AWTBOF90L00IqeW03FceqefNESgaYwL1hKZ1OgWCMrbVESoiLQqSA0uWFxtyLV4qjx5S2nGfPogyKme%2BwEfNIB%2FyEimnCew49VXktRtc8uUlUl130kg1piVQnghwdxbTECD1iiWr9cpQZmNMObRwswGOqUBDL0d7HbqTE6ofn%2BxAKhthsTh7Z3WzyufWahj3R8TdqCz8JYvVWuHf0hnOfsw0le3DundL91nLMajflNBhwZ26mbkFaz%2Fa3K5C%2BGVbD%2BYDb5p6GZYP%2FIBELsV9P5ykX%2BFKTosijQzMvi3JYJ6YZq1ggUS%2B%2FuXJDfgtIcgFH4jRPrKX1sOwl0D1MrNXuszsiiGa0%2Fxq8GZUSFTkTZhVF%2BKh%2BEI7G0L&X-Amz-Signature=d0bd0229e8d658b2f18d3d0af2e06a7c29138f6909748e644b7506946de08fdf&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject) Rails link_to method! One of the most common helper methods you’ll use in all your Rails applications. But how does it work? If you came here to learn about link_to & the different options you can use with it then you’re in the right place! First… What does link_to do? Well, this whole website thing only works because we have links between pages. That’s how you go from page to page. In plain HTML, you create a link like this: But in Rails that would look like this: ```plain text <%= link_to "Improve Your Ruby Skills", "/ruby-book" %> ``` Why? Because we use routes in Rails. We want to take advantage of that by using _path methods & making that the target (href) of our link. Using link_to makes this easier because we don’t have to interpolate the value. This is what I mean: Now: Let’s look deeper into the most helpful optional arguments supported by this method. ## Rails link_to Options & Arguments The first argument for link_to is the text on the link. The second argument? It’s the URL you’re linking to. You can hardcode it if you want, but most of the time you’ll be using a Rails model, or a _path method. Rails will figure things out when you follow the proper conventions. For example: ```plain text <%= link_to "Improve Your Ruby Skills", book_path(@book) %> ``` Or: ```plain text <%= link_to "Improve Your Ruby Skills", @book %> ``` How do you know which one to use? - Singular form when referring to a specific resource (book) - Plural form when referring to a collection (books) Examples: ```plain text # Plural <%= link_to "All Books", books_path %> # Singular <%= link_to "Edit Book", edit_book_path(@book) %> ``` It also helps to look at your routes (with rake routes). The 1st column has the name (like edit_book) that you can use in your links by adding _path to it. ## Delete Link With Confirmation Two helpful options you can use: - confirm - disable_with Example: ```plain text <%= link_to "Delete Book", @book, method: "delete", { confirm: "Are you sure?", disable_with: "Processing..." } %> ``` The default action for a link is a GET request. If you want to use the “DELETE” action you need to be specific about it. But… What if you want a link to the previous page? Try this: ```plain text <%= link_to "Back", :back %> ``` ## How to Use Custom CSS Classes & HTML Attributes You can make your links look different using CSS. For example: ```plain text <%= link_to "Get More Books", books_path, class: "index-link" %> ``` Assuming you have this CSS: ```plain text .index-link { color: blue; padding: 10px; } ``` If you need more HTML attributes (like “id”) you can add them after the link text & link URL. ## How to Create Query Params & Anchor Links Another option you may find helpful is the ability to create links with query parameters & anchors. Why is that helpful? Because you’ll be able to access this additional data from your controller after the user clicks the link. Let’s look at some examples! If you want to create an URL like this: ```plain text "/search?q=all" ``` Then you can do this: ```plain text <%= link_to "Search ALL", search_path(q: "all") %> ``` And for this URL: ```plain text "/books#programming" ``` You can do this: ```plain text <%= link_to "Programming Books", books_path(anchor: "programming") %> ``` Combined with URL helpers (_path / _url), link_to gives you all the flexibility you need. ## Linking to Images Many people don’t know that link_to takes an optional block. This enables more complex scenarios. Like linking to an image: ```plain text <%= link_to books_path do %> <%= image_tag "Book Collection" %> <% end %> ``` In this example, the content of the block becomes the linking text, image or any other HTML element that you want to become clickable. ## Summary You’ve learned about the link_to method in Rails! If you found this article helpful get a copy of my Ruby book so you can super-charge your Ruby skills & support my work. Thanks for reading 🙂 You forgot including format and array style like link_to [@book, format: :pdf]. Thanks for your work

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation