简体   繁体   中英

How to pass params in link to - ruby on rails

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>  

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

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

如果您设置了 @category 变量,这将带您到 /article_categories/:id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM