I'm doing a site at the moment (first Rails site, learning as I code) and I'm concerned that I'm over complicating the routes.
My associations boil down to:
class Recipe < ActiveRecord::Base
has_and_belongs_to_many :categories
belongs_to :book
end
class Category < ActiveRecord::Base
belongs_to :category_type
has_and_belongs_to_many :recipes
end
class Book < ActiveRecord::Base
has_many :recipes
end
I want to end up with URLs like this:
/books/3/recipes #to show all recipes from book 3
/category/12/recipes #to show all categories from category 12
/recipes/3 #to show recipe 3
The good news is I have this all working, but in the course of doing so I feel like I may have strayed a little from "the Rails way" and I was wondering if someone could take a look and let me know if I've gone wrong. The bits I'm particularly concerned about are my routes.rb file (because I appear to be repeating myself):
resources :books do
resources :recipes
end
resources :categories do
resources :recipes
end
In particular, the recipes#index
action, which seems a little verbose:
def index
if(params[:category_id])
@recipes = Recipe.find_by_category(params[:category_id])
elsif(params[:book_id])
@recipes = Recipe.where(:book_id => params[:book_id])
else
@recipes = Recipe.find(:all)
end
end
Now, it might be that this is all absolutely fine, but I just want to check before I get too much further in!