嗨,我目前正在用 rails (3) 构建一个小论坛应用程序。我在 Rails 问题上相当新,我被这些话题困住了。
我有 2 个模型(主题和主题回复)
主题模型:
class Topic < ActiveRecord::Base
belongs_to :board
belongs_to :user
has_many :topic_replies, :dependent => :destroy
TOPIC_TYPES = ["Non-support", "Question"]
validates :topic_type, :inclusion => TOPIC_TYPES
end
主题回复模型:
class TopicReply < ActiveRecord::Base
belongs_to :topic
belongs_to :user
end
当我创建一个主题时,除了 topic_replies (主题内的所有帖子)外,一切都显示正常
原因是:我的 topic_reply 对象正确保存了所有内容,只是它没有将 topic_id 保存在对象中,因此也保存在我的数据库中。
这是我的主题控制器的一部分,用于创建主题:
# GET /topics/new
# GET /topics/new.xml
def new
@topic = Topic.new
@topic_reply = @topic.topic_replies.build
respond_to do |format|
format.html # new.html.erb
end
end
# POST /topics
# POST /topics.xml
def create
@topic = Topic.new(params[:topic])
@topic.id_board = @board.id
@topic_reply = @topic.topic_replies.build(params[:topic_reply])
@topic_reply.id_topic = @topic.id
@topic_reply.id_poster = User.first.id
respond_to do |format|
if @topic.save && @topic_reply.save
format.html { redirect_to(topic_path("#{@board.name.parameterize}-#{@board.id}", "#{@topic.title.parameterize}-#{@topic.id}")) }
else
format.html { render :action => "new" }
end
end
end
如果我发布的信息太少,是否有人知道我做错了什么让我知道我会添加您需要的内容。
提前致谢