user1107922
user1107922

Reputation: 610

Rails routing two controllers

Firstly, I want to tell you that I am a novice in rails and I have a stupid question. I want to make an application where I should post news and every new will have have a category. So I create a controller about categories. Now, I add, edit and delete categories and I should create a controller about news but how should I connect news with categories in routes? I hope you understand my question. Thanks in advance!

Upvotes: 0

Views: 89

Answers (1)

alf
alf

Reputation: 18530

Assuming a story can only have one category, the model would be:

class Category < ActiveRecord::Base
  has_many :stories
end

class Story < ActiveRecord::Base
  belongs_to :category
end

From the routing point of view, you can nest the resources:

resources :categories do
  resources :stories
end

or not:

resources :categories
resources :stories

This choice is up to you :) See Nested resources

Upvotes: 1

Related Questions