ID在URL中重复

时间:2018-04-02 16:41:38

标签: ruby-on-rails

对我很轻松 - 只使用rails一周了!

/作者/的 98 / 5 /书籍/编辑/ 98

^^^我无法摆脱不必要的第一本书ID(98)

我的路线>>>

resources :author do
  member do
    get '/books', to: 'author_books#index'
    post '/books', to: 'author_books#create'
    patch '/books', to: 'author_books#update'
    delete '/books', to: 'author_books#destroy'
    get '/books/new', to: 'author_books#new'
    get ':author_id/books/edit/:id', to: 'author_books#edit', as: 'edit_book'
  end
end

链接>>>

<td><%= link_to 'Edit', edit_book_home_author_path(f, @author) %></td>

提前感谢您的任何帮助: - &gt;

1 个答案:

答案 0 :(得分:1)

您的路线过于陈述且冗长。更多地依靠资源丰富的路由:

resources :authors, shallow: true do
  resources :author_books, path: "books"
end

这将创建如下的路由处理程序:

/authors          # => AuthorsController#index
/authors/42/edit  # => AuthorsController#edit
/authors/42/books # => AuthorBooksController#index
/authors/books/7  # => AuthorBooksController#show

你可以使用这样的助手:

link_to "Edit Book", edit_author_book_path(@book)
link_to "Edit Author", edit_author_path(@author)

点击the docs查看resources上的细节,并参阅Rails指南中的Routing From The Outside In,以获得精彩的概述和介绍。