EastsideDev
EastsideDev

Reputation: 6639

Creating a link to a rails route

Rails 5.2.3

In my routes.rb, I have:

get '/books/:author', to: 'books#index'
get '/books/:author/show', to: 'books#show'
post '/books/:author/create', to: 'books#create'

Which, when running rake: routes, gives me:

GET    /books/:author(.:format)  books#index
GET    /books/:author/show(.:format)  books#show
POST   /books/:author(.:format)  books#create

When a route helper is not provided by rake: routes, can I assume that when I create a link to it in a view, I can use use the model name, like: books_path or books_url? something like:

= link_to books_path(:author => @author), :method => :post

I tried it, but I am getting an error message:

undefined method books_path

So, I am either doing something wrong in routes.rb, or I am not referencing it correctly?

Upvotes: 1

Views: 507

Answers (3)

jvillian
jvillian

Reputation: 20263

Just to pile on, I suggest you use nested routes:

resources :authors do 
  resources :books, shallow: true 
end
resources :books, only: [:index, :create, :new]

Which will give you (amongst other things):

   author_books GET    /authors/:author_id/books(.:format)      books#index
                POST   /authors/:author_id/books(.:format)      books#create
new_author_book GET    /authors/:author_id/books/new(.:format)  books#new
      edit_book GET    /books/:id/edit(.:format)                books#edit
           book GET    /books/:id(.:format)                     books#show
                PATCH  /books/:id(.:format)                     books#update
                PUT    /books/:id(.:format)                     books#update
                DELETE /books/:id(.:format)                     books#destroy
        authors GET    /authors(.:format)                       authors#index
                POST   /authors(.:format)                       authors#create
     new_author GET    /authors/new(.:format)                   authors#new
    edit_author GET    /authors/:id/edit(.:format)              authors#edit
         author GET    /authors/:id(.:format)                   authors#show
                PATCH  /authors/:id(.:format)                   authors#update
                PUT    /authors/:id(.:format)                   authors#update
                DELETE /authors/:id(.:format)                   authors#destroy
          books GET    /books(.:format)                         books#index
                POST   /books(.:format)                         books#create
       new_book GET    /books/new(.:format)                     books#new

That ought to give you everything you need to manage authors and books.

Upvotes: 2

Trinculo
Trinculo

Reputation: 2021

You are creating an unnamed route. I think what you want in your particular situation is:

  resources :books, only: [:index, :show, :create], param: :author

That will give:

                   Prefix Verb URI Pattern                                                                              Controller#Action
                    books GET  /books(.:format)                                                                         books#index
                          POST /books(.:format)                                                                         books#create
                     book GET  /books/:author(.:format)                                                                 books#show

This does not seem RESTful to me though. I think what you really want is a nested resource between books and authors. Check out the Rails guides on routing for more information: https://guides.rubyonrails.org/routing.html

Upvotes: 2

arieljuod
arieljuod

Reputation: 15838

I don't get how post '/books/:author/create', to: 'books#create' gives you POST /books/:author(.:format) books#create.

If you want to name your custom routes add the name with the :as option: get 'something', to: 'controller#action', as: 'something' to get "something_path" as a valid helper.

Note that you are going away from RESTful routes, if you want rails to do the things it usually do (like the named routes helpers) you should stick with it's conventions: https://guides.rubyonrails.org/routing.html

Upvotes: 0

Related Questions