2

How should I pass the params about the article category. I have a Article which belongs to Article category. When I click to this <%= link_to @article.name, article_categories_path%>, I want to see the all articles in this category. So I have to pass the params through it to ArticleCategoryController. Is my link to statement correct ? Now I get a "Article" instead of the category name( when I click it I'm getting the error: Document.find expects the parameters to be 1 or more ids,).

Article model

class Article
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title, type: String
  field :content, type: String

  belongs_to :user
  #kategorie
  belongs_to :article_category

Article controler

  class ArticlesController < ApplicationController
    def article
    @article = Article.order_by(created_at: 'desc').page params[:page]
  end

  def view_article
    @article = Article.find(params[:id])
  end
end

ArticleCategory model

class ArticleCategory
  include Mongoid::Document
  include Mongoid::Timestamps


  field :name, type: String

  has_many :articles


end

Routes

  resources :article_categories do
  resources :articles, shallow: true
  end

ArticleCategories Controller

class ArticleCategoriesController < ApplicationController

   def index
     @category = ArticleCategory.find(params[:id])
     @articles = @category.articles
   end
 end

Article view

<p>Category: <%= link_to @article.name, article_categories_path%>
            Tagi: <%= link_to "Tagi", tags_path %> </p>  
steve klein
  • 2,566
  • 1
  • 14
  • 27

2 Answers2

1

the way I would normally do something like this is with a has many through. (also I would check your models, since it looks like you have the belongs_to/has_many backwards, belongs_to goes in the model with foreign keys)..

class Article
   has_many :article_categories
   has_many :categories, through: :article_categories
end

class ArticleCategory
   belongs_to: :article
   belongs_to: :category
end

class Category
   has_many :article_categories
   has_many: articles, through: :article_categories
end 

Then if you would like to see all Articles in a given category. then you can do it via the CategoriesController

class CategoriesController < ApplicationController
    ....
    def show
       @category = Category.find params[:id]
    end 

 end

and then you should be able to get all the articles via @category.articles association.

Since the way you are doing it now you will only get the 1 category via the article_category table.

In response to comment..

Each article is going to have multiple categories..

So you would have something like this.

 <% @article.categories.each do |cat| %>
   <%= link_to cat.name, cat %> <br />
 <% end %>

Formatted however you would like. This assumes you have a resources :categories in your routes, and Category has a name field

Doon
  • 19,719
  • 3
  • 40
  • 44
  • Good idea, so now how should i link to the category in the Article view? – Kamil Mrozek Sep 10 '15 at 19:21
  • After update i'm getting Mongoid Problem: I`nvalid option :through provided to relation :categories.` – Kamil Mrozek Sep 10 '15 at 20:40
  • Ahh looks like mongoid doesn't support that. http://stackoverflow.com/questions/7000605/how-to-implement-has-many-through-relationships-with-mongoid-and-mongodb – Doon Sep 10 '15 at 21:00
0

<%= link_to "All", article_categories_path(@category) %>

Provided you've got @category variable set, this will take you to /article_categories/:id

TomD
  • 781
  • 4
  • 17