0

我正在构建论坛 rails 应用程序,我遇到了嵌套资源的问题。

这里是controller/school/forums_controller.rb

  def index
    @forums = Forum.all
  end


  def show
    @forum = Forum.find(params[:id])
    @per_page = params[:per_page] || 15
    @topics = @forum.topics.search(params[:search]).paginate(:per_page => @per_page, :page => params[:page])
  end

这里是controller/school/topics_controller.rb

  def new
    @forum = Forum.find(params[:forum_id])
    @topik = @forum.topiks.build
  end

  def create
    @forum = Forum.find(params[:forum_id])
    @topic = @forum.topics.build(params[:topic])
       if @topic.save
          redirect_to school_forum_topic_path(@topic)
        else
          render :action => "new"
        end
  end

  def show
    @forum = Forum.find(params[:forum_id])
    @topic = Topic.find(params[:id])
  end

这里是routes.rb

get '/forum' => 'school/forums#index', :as => :forum_subdomain

namespace :school, :path => '/' do
        resources :forums, :path => '/forum', :only => [:show] do
          resources :topics
        end
end


 forum_subdomain GET    /forum(.:format)
                        school/forums#index {:subdomain=>/.+/}
                    school_forum_topics GET    /fr/:forum_id/topics(.:
format)                 school/topics#index {:subdomain=>/.+/}
                                           POST   /forum/:forum_id/topics(.:
format)                 school/topics#create {:subdomain=>/.+/}
                 new_school_forum_topic GET    /forum/:forum_id/topics/ne
w(.:format)             school/topics#new {:subdomain=>/.+/}
                edit_school_forum_topic GET    /forum/:forum_id/topics/:i
d/edit(.:format)        school/topics#edit {:subdomain=>/.+/}
                     school_forum_topic GET    /forum/:forum_id/topics/:i
d(.:format)             school/topics#show {:subdomain=>/.+/}
                                           PUT    /forum/:forum_id/topics/:i
d(.:format)             school/topics#update {:subdomain=>/.+/}
                                           DELETE /forum/:forum_id/topics/:i
d(.:format)             school/topics#destroy {:subdomain=>/.+/}
                           school_forum GET    /forum/:id(.:format)
                        school/forums#show {:subdomain=>/.+/}

当我访问subdomain.lvh.me:3000/forum/1-room-biology

Routing Error

No route matches {:action=>"new", :controller=>"school/topics"}

这里是views/school/forums/show.html.erb

<%= link_to new_school_forum_topic_path , :class => "btn btn-inverse btn-medium" do %>
 New Topic
<% end %>

<% for topic in @topics %>
<%= link_to topic.title, school_forum_topic_path(topic) %>
<% end %>

我该如何正确地做到这一点和/或路由所有这些的正确方法是什么?

4

1 回答 1

0

您的电话new_school_forum_topic_path需要提供论坛,例如。new_school_forum_topic_path(@forum).

有关 Rails 中嵌套资源路由的更多信息:http: //guides.rubyonrails.org/routing.html#nested-resources

于 2013-04-27T08:08:06.063 回答