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! :)"

# 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

# link_to によって生成されるもの:

"/helpcenter/cat/1-first-topic/topics/1-first-forum"

# 私が期待していたもの (トピックはフォーラムにあるはずなので):

"/helpcenter/cat/1-first-forum/topics/1-first-topic"

# 何が間違っているのでしょうか?

puts "Thanks!"
4

2 に答える 2

1

こんにちは私はあなたが特別なフォーラムの特別なトピックへのリンクを生成したいと思います。

したがって、リンクを生成するときは、2つのID(-または2つのインスタンス、1つはフォーラム用、もう1つはトピック用)が必要です。

link_to @forum_topic.name, forum_topic_path(@forum, @forum_topic)
于 2012-06-29T12:35:09.783 に答える
1

プライマリパスは次のようになります。

forum_topic_path(@forum, @topic)  
forum_topics_path(@forum)

渡したパラメータ(@forum_topic)

link_to @forum_topic.name, forum_topic_path(@forum_topic)

1つの:idが関連付けられているため(1)、id=1のフォーラムが表示されます。トピックIDも渡す必要があります

forum_topic_path(@forum, @topic)

idがないとエラーが発生しなかったことに驚いています。両方のリソースに:idが推測されたと思います。

于 2012-06-29T12:31:02.003 に答える