1
#解釋上下文
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! :)"
#配置/ 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
#應用程序/模型/ 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
#應用程序/控制器/論壇/ 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!"
事實上,我沒有得到任何錯誤。謝謝(也感謝@Alper)。由於某些原因,我讀過的在線資源中沒有提到將兩個對象傳遞給_path助手。現在就像魅力一樣。感謝您獲得接受答案的額外信息! – roychri