如何通过关于文章类别的参数。我有一篇文章属于文章类别。当我点击这个<%= link_to @article.name, article_categories_path%>
,我想看看这个类别中的所有文章。所以我必须通过它将参数传递给ArticleCategoryController。我的声明链接是否正确?现在我得到一个“文章”,而不是类别名称(当我点击它时,我收到错误:Document.find expects the parameters to be 1 or more ids,
)。如何在链接中传递参数 - ruby on rails
条模型
class Article
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :content, type: String
belongs_to :user
#kategorie
belongs_to :article_category
第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模型
class ArticleCategory
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
has_many :articles
end
ř欧特斯
resources :article_categories do
resources :articles, shallow: true
end
ArticleCategories控制器
class ArticleCategoriesController < ApplicationController
def index
@category = ArticleCategory.find(params[:id])
@articles = @category.articles
end
end
文章观点
<p>Category: <%= link_to @article.name, article_categories_path%>
Tagi: <%= link_to "Tagi", tags_path %> </p>
你的路线是什么样的。 – Doon
为什么你要在你的'link_to'中发送一个文章名作为你的第一个参数?不知道你打算做什么。 –