我有一些麻煩。 我有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