开发者

Rails 3 - Dealing with Model Inheritance? Books > Authors, not Books & Authors

开发者 https://www.devze.com 2023-01-18 17:24 出处:网络
I have two models: Books and Authors. Books has_many Authors Authors belongs_to Books Problem is right now this loads in my app:

I have two models: Books and Authors. Books has_many Authors Authors belongs_to Books

Problem is right now this loads in my app: /books /authors

I don't want users to be able to look at /autho开发者_Go百科rs

The behavior I want is users to first select a Book and get all kinds of great book detail... Then the user can click "Show me all the book's authors" to go to something like: /books/10/authors/

And since authors can't exist outside of a book, to view a author: /books/10/authors/1

And in this case, authors can't belong to multiple books, just one book (trust me!)

Solutions, Ideas? thank you


I would look at nested resources for your routes. Your can define this in your routing file like so:

resources :books do
  resources :authors
end

Now users won't be able to access just /authors. They will need to go to /books/1/authors

More here: http://edgeguides.rubyonrails.org/routing.html#nested-resources

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号