Juan Artau
Juan Artau

Reputation: 357

Model Association

Hi I have a little problem with associations, I'm working with Rails 3.2, I want to create a special blog, this blog has many sections and this sections has many articles and an article belongs to a Category. So my models are:

class Article < ActiveRecord::Base
  belongs_to :section
  belongs_to :category

class Category < ActiveRecord::Base
  has_many :articles

class Section < ActiveRecord::Base
  has_many :article

So articles belongs_to section, in routes.rb :

 resources :sections do
   resources :articles
 end

Rake Routes:

                     POST   /sections/:section_id/articles(.:format)          articles#create
 new_section_article GET    /sections/:section_id/articles/new(.:format)      articles#new
edit_section_article GET    /sections/:section_id/articles/:id/edit(.:format) articles#edit
     section_article GET    /sections/:section_id/articles/:id(.:format)      articles#show
                     PUT    /sections/:section_id/articles/:id(.:format)      articles#update
                     DELETE /sections/:section_id/articles/:id(.:format)      articles#destroy
            sections GET    /sections(.:format)                               sections#index
                     POST   /sections(.:format)                               sections#create
         new_section GET    /sections/new(.:format)                           sections#new
        edit_section GET    /sections/:id/edit(.:format)                      sections#edit
             section GET    /sections/:id(.:format)                           sections#show
                     PUT    /sections/:id(.:format)                           sections#update
                     DELETE /sections/:id(.:format)                           sections#destroy

So my question is How do I create a Categories_controller with index and show action. to show the articles that belongs to that category, and have a link_to in views(Category#show) for the articles path.

Upvotes: 0

Views: 190

Answers (2)

Cade
Cade

Reputation: 3181

Assuming that you do need nested routes to fit your domain model (ex. articles need to display differently based on whether they are being viewed in the context of a category or a section), then you can just create another set of nested routes like so:

routes.rb

resources :categories, only: [:index, :show] do
  resources :articles
end

That will set up routes to find your categories and articles by categories, but you will have to fork your logic in your ArticlesController on params[:category_id] or params[:section_id] like so:

class ArticlesController < ApplicationController
  def index
    if params[:section_id]
      # handle section_articles_path to display articles by section
    elsif params[:category_id]
      # handle category_articles_path to display articles by category
    else
      # handle articles_path to display all articles (assuming you have resources :articles in routes)
    end
  end
  # ...
end

The link to show an article based on a category will then use the generated route helper methods created for you.

categories/show.html.erb

<ul>
  <% @category.articles.each do |article| %>
    <li><%= link_to article.title, category_article_path(@category, article) %></li>
  <% end %>
</ul>

You could then, of course, refactor the view code to render the collection as its own partial rather than manually iterating, but I'll leave it at that for now...

If you don't need to handle the context differently (accessing an article through a section versus a category), I'd recommend just setting up three basic routes (:articles, :sections, :categories) and going from there.

Upvotes: 1

Amar
Amar

Reputation: 6942

It's Very much simple match 'catagories' => "catagories#index" and match 'catagories/show/:id' => "catagories#show" In show action @articles = Article.where("category_id",params[:id])

@articles = Article.where("category_id",params[:id]) This will solve your purpose

Upvotes: 0

Related Questions