# 解释上下文
puts "I am learning Rails, building a simple forum application."
puts "I am pretty satisfied to where I got so far but routes... "
puts "...still figuring them out."
puts "Been 2 days trying all sorts of things."
puts "This is where I am now, and something is not working as expected."
puts "Any help/pointers would be appreciated! :)"
# 配置/路由.rb
scope "/helpcenter" do
resources :cat, :controller => "forums", :as => :forums do
resources :topics , :controller => "forum_topics", :as => :topics
resources :posts, :controller => "forum_posts", :as => :posts
end
end
match "/helpcenter" => "forums#index", :as => :forums
# app/models/forum.rb
class Forum < ActiveRecord::Base
def to_param
"#{id}-#{name.parameterize}"
end
end
# 应用程序/模型/forum_topic.rb
class ForumTopic < ActiveRecord::Base
def to_param
"#{id}-#{name.parameterize}"
end
end
# 应用程序/控制器/论坛/show.hmtl.erb
link_to @forum_topic.name, forum_topic_path(@forum_topic)
# OR
link_to @forum_topic.name, @forum_topic
# link_to 正在生成什么:
"/helpcenter/cat/1-first-topic/topics/1-first-forum"
# 我期望看到的(因为主题应该在论坛中):
"/helpcenter/cat/1-first-forum/topics/1-first-topic"
# 我究竟做错了什么?
puts "Thanks!"