嵌套资源链接错误

时间:2015-10-05 10:13:40

标签: ruby-on-rails nested link-to

我遇到了麻烦。 我有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如何创建这个是嵌套链接...帮帮我

2 个答案:

答案 0 :(得分:0)

以下是如何使其发挥作用:

Intent i = new Intent(this, myclass.class);

答案 1 :(得分:0)

你可以试试这个

[tapHandler setCancelsTouchesInView:NO];

并在视野中

resources :forums do
   resources :topics
end

并在控制器中

- @topics.each do |f|
  = link_to f.name, forum_topic_path(f.forum.id, f.id)