user3379926
user3379926

Reputation: 3945

Routes not looking right. Missing id and index

In a Rails 4.1 app I created a set of routes, to avoid severe nesting in this type of relationship:

User has_and_belongs_to_many :blogs, join_table: 'blogs_users'
User has_many :posts, through: :blogs

Blogs has_and_belongs_to_many :users, join_table: 'blogs_users'
Blogs has_many :posts

Posts has_and_belongs_to_many :tags, join_table: 'tags_posts'
Posts has_and_belongs_to_many :categories, join_table: 'categories_posts'
Posts has_many :comments
Posts belongs_to :user
Posts belongs_to :blogs

Comments belongs_to :posts

It's rather simple, how ever the rotes look as such (I was advised this was easier):

  namespace :api do
    namespace :v1 do

      resource :users, only: [:index, :show] do
        resource :blogs, only: [:index, :show]
      end

      resource :blogs, only: [:index, :show] do
        resource :posts, only: [:index, :show]
      end

      resource :posts, only: [:index, :show] do
        resource :tags
        resource :comments
        resource :categories
      end

    end
  end

The rake routes command spits out:

             Prefix Verb   URI Pattern                             Controller#Action
  api_v1_users_blogs GET    /api/v1/users/blogs(.:format)           api/v1/blogs#show
        api_v1_users GET    /api/v1/users(.:format)                 api/v1/users#show
  api_v1_blogs_posts GET    /api/v1/blogs/posts(.:format)           api/v1/posts#show
        api_v1_blogs GET    /api/v1/blogs(.:format)                 api/v1/blogs#show
   api_v1_posts_tags POST   /api/v1/posts/tags(.:format)            api/v1/tags#create

# This is just a few to keep the post clean and manageable. I can gist the whole output if you require it.

As you can see I am missing id and index I am sure my mistake is something rudimentary How ever I am lost as to what ...

I am also concerned about how the user accessing the api will create posts as I am only doing an API Key Authentication. I mean I suppose I could create a "current_user" based off that use that to pass in the user id to the posts, when one is created.

My questions are:

Upvotes: 0

Views: 60

Answers (1)

apneadiving
apneadiving

Reputation: 115541

You should replace resource with resources.

The plural has much meaning: there can be many, hence the need for ids, the existence of index etc...

See documentation

Upvotes: 4

Related Questions