我正在构建论坛rails app,我对嵌套资源有疑问。
这里是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 %>
如何正确地执行此操作和/或正确的方法是什么?
答案 0 :(得分:0)
您对new_school_forum_topic_path
的来电需要提供论坛,例如。 new_school_forum_topic_path(@forum)
。
有关Rails中嵌套资源路由的更多信息:http://guides.rubyonrails.org/routing.html#nested-resources