我有一些麻烦。 我有3种型号:论坛帖子 论坛嵌套资源链接错误
has_many :topics, dependent: :destroy
主题
belongs_to :forum
has_many :posts, dependent: :destroy
发表belongs_to的:话题
论坛控制器
class ForumsController < ApplicationController
def index
@forums = Forum.all
end
def show
@forum = Forum.find(params[:id])
@topics = Topic.all
end
end
主题控制器
class TopicsController < ApplicationController
def create
@forum = Forum.find(params[:forum_id])
@topic = @forum.topics.create(topic_params)
if @topic.save
redirect_to root_path
end
end
def new
@forum = Forum.find(params[:forum_id])
@topic = Topic.new
end
def show
@forum = Forum.find(params[:forum_id])
@topics = Topic.find(params[:id])
end
private
def topic_params
params.require(:topic).permit(:name, :created_at, :last_poster_id => current_user.id, :last_post_at => Time.now)
end
末
routes.rb
resources :forums do
resources :topics
end
论坛/显示
- @forum.topics.each do |f|
= link_to f.name, forum_topic_path[@forum, @topic]
rake routes:
forum_topics GET /forums/:forum_id/topics(.:format) topics#index
POST /forums/:forum_id/topics(.:format) topics#create
new_forum_topic GET /forums/:forum_id/topics/new(.:format) topics#new
edit_forum_topic GET /forums/:forum_id/topics/:id/edit(.:format) topics#edit
forum_topic GET /forums/:forum_id/topics/:id(.:format) topics#show
PATCH /forums/:forum_id/topics/:id(.:format) topics#update
PUT /forums/:forum_id/topics/:id(.:format) topics#update
DELETE /forums/:forum_id/topics/:id(.:format) topics#destroy
forums GET /forums(.:format) forums#index
POST /forums(.:format) forums#create
new_forum GET /forums/new(.:format) forums#new
edit_forum GET /forums/:id/edit(.:format) forums#edit
forum GET /forums/:id(.:format) forums#show
PATCH /forums/:id(.:format) forums#update
PUT /forums/:id(.:format) forums#update
DELETE /forums/:id(.:format) forums#destroy
,但我有错误
No route matches {:action=>"show", :controller=>"topics", :id=>"1"} missing required keys: [:forum_id]
IDN如何创建此嵌套链接...帮助我
能否请您发布的内容你的routes.rb文件? 此外,如果你可以粘贴'耙路线'的输出。 – Dusht