我有麻烦了。我有三种模式:论坛主题后论坛
has_many :topics, dependent: :destroy主题
belongs_to :forum
has_many :posts, dependent: :destroyPost 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如何创建这是嵌套链接..。帮帮我
发布于 2015-10-05 11:02:39
下面是如何让它发挥作用的方法:
#config/routes.rb
resources :forums do
resources :topics
end
#app/models/forum.rb
class Forum < ActiveRecord::Base
has_many :topics
end
#app/models/topic.rb
class Topic < ActiveRecord::Base
belongs_to :forum
end
#app/controllers/forums_controller.rb
class ForumsController < ApplicationController
def show
@forum = Forum.find params[:id]
@topics = @forum.topics
end
end
#app/views/forums/show.html.erb #-> url.com/forums/5
<% @topics.each do |topic| %>
= link_to topic.name, forum_topic_path(@forum.id, topic.id)
<% end %>发布于 2015-10-05 11:10:51
你可以试试这个
resources :forums do
resources :topics
end以及在视野中
- @topics.each do |f|
= link_to f.name, forum_topic_path(f.forum.id, f.id)在控制器中
def show
@forum = Forum.find(params[:id])
@topics = @forum.topics
endhttps://stackoverflow.com/questions/32946078
复制相似问题