# Explaining the context
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! :)"
# config/routes.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
# app/models/forum_topic.rb
class ForumTopic < ActiveRecord::Base
def to_param
"#{id}-#{name.parameterize}"
end
end
# app/controllers/forums/show.hmtl.erb
link_to @forum_topic.name, forum_topic_path(@forum_topic)
# OR
link_to @forum_topic.name, @forum_topic
# What is being generated by link_to :
"/helpcenter/cat/1-first-topic/topics/1-first-forum"
# What I expected to see (because topics should be in forums):
"/helpcenter/cat/1-first-forum/topics/1-first-topic"
# What am I doing wrong?
puts "Thanks!"
The primary paths should be:
The param (@forum_topic) you passed in
Has one :id associated with it (1), so it will show the forum with id=1. You should also be passing the topic id
Surprised you didn’t get an error without the id–I am guessing that it inferred the :id to both resources.