Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:30:26+00:00 2026-06-12T22:30:26+00:00

Learning ruby on rails, playing around with routes and path methods. I have built

  • 0

Learning ruby on rails, playing around with routes and path methods.
I have built a fairly simple application with the models “Book” and “Author”. Each author can have many books, every book belongs to an author, etc.

I have an authors controller with the method #list that lists all authors. When it does that, it also echoes out all their books. Now I would like each book to point to books#show with this nifty path method I found.

Here is my snippet:

<% @authors.each do |author| %>
    <li><%= author.name %></li>
    <ul>
        <% author.books.all.each do |book| %>
            <li><%= link_to book.name, controller: :books, action: :show, id: book.id %></li>
        <% end %>
    </ul>
<% end %>

With this link_to book.name, controller: :books, action: :show, id: book.id it works fine, but something tells me the same can be achieved much easier. If I use books_show_path(book) it won’t work.

My routes look like the following:

root to: ‘authors#list’

match 'authors/list' => 'authors#list'
match 'books/list' => 'books#list'
match 'authors/:id/delete' => 'authors#delete'
match 'authors/:id/show' => 'authors#show'
match 'books/:id/show' => 'books#show'

What am I doing wrong here? What do I need to do to make books_show_path(book) work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:30:27+00:00Added an answer on June 12, 2026 at 10:30 pm

    Check out Resource Routing section of Rails Guides.

    In short, the convention is that you define resources :books in routes, that will route /books to index action and /books/123 to show action etc. You will also have some helper methods to generate the path:

    book_path(book) #=> /books/123
    books_path #=> /books
    

    In fact, link_to and a bunch of other methods can generate path from object itself:

    link_to book.name, book # will also call book_path(book) under the hood
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning Ruby on Rails (no prior Ruby experience) I have these models
I'm learning Ruby on Rails with the AWDR book and have had to be
As part of learning ruby/rails, I'm trying to implement http://github.com/professionalnerd/simple-private-messages into my application from
I am coming from the world of PHP and am learning Ruby/Rails. I have
I'm just learning ruby on rails and I have a table of user roles
While learning Rails, I found this example in the Sam Ruby's book: app/controllers/line_items_controller.rb def
I am still learning Ruby on Rails, but have a general question about using
I'm starting learning Ruby on Rails. So my application, like every Rails app, has
I'm working on learning ruby/rails at the moment - The book i'm reading has
I am learning Ruby on Rails and I have a dumb question about the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.