如何通過關於文章類別的參數。我有一篇文章屬於文章類別。當我點擊這個<%= 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'中發送一個文章名作爲你的第一個參數?不知道你打算做什麼。 –