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! :)"
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
class Forum < ActiveRecord::Base
def to_param
"#{id}-#{name.parameterize}"
end
end
class ForumTopic < ActiveRecord::Base
def to_param
"#{id}-#{name.parameterize}"
end
end
link_to @forum_topic.name, forum_topic_path(@forum_topic)
# OR
link_to @forum_topic.name, @forum_topic
"/helpcenter/cat/1-first-topic/topics/1-first-forum"
"/helpcenter/cat/1-first-forum/topics/1-first-topic"
puts "Thanks!"
答案 0 :(得分:1)
主要路径应为:
forum_topic_path(@forum, @topic)
forum_topics_path(@forum)
你传入的参数(@forum_topic)
link_to @forum_topic.name, forum_topic_path(@forum_topic)
有一个:id与之关联(1),因此它将显示id = 1的论坛。您还应该传递主题ID
forum_topic_path(@forum, @topic)
感到惊讶的是没有id没有得到错误 - 我猜测它推断了两个资源的id。
答案 1 :(得分:1)
您好我想要生成指向特殊论坛特殊主题的链接。
当你生成链接时,你需要2个ID( - 或2个intances,一个用于论坛,一个用于主题)。
link_to @forum_topic.name, forum_topic_path(@forum, @forum_topic)