1

我构建的应用程序允许用户创建问题类型:True/False、Single 和 Multiple Choice。所以,我创建了一些模型:

class QuestionType < ActiveRecord::Base
  attr_accessible :name, :shorcut
end

class Question < ActiveRecord::Base
  attr_accessible :content, :mark, :topic_id, :question_type_id, :answers_attributes
  belongs_to :topic
  belongs_to :user
  belongs_to :question_type
  has_many :answers, dependent: :destroy
  accepts_nested_attributes_for :answers
end

class Answer < ActiveRecord::Base
  attr_accessible :content, :question_id, :correct

  belongs_to :question
end

现在我要建立一个问题的索引页面,有 3 个链接来为 3 种类型的问题添加问题,当用户单击一个链接时,他们将转到页面以创建问题,并且页面将具有适合该问题类型的表格。在问题控制器中,我想存储问题类型 ID 以保存到问题。
我认为地址是这样的:

http://example.com/questions/index:索引页面将有 3 个链接来创建问题。

http://example.com/question_types/1/questions/new:将为真/假问题呈现部分形式

http://example.com/question_types/2/questions/new:将为单选问题呈现部分形式

http://example.com/question_types/1/questions/new:将为多项选择问题呈现部分形式

我认为我必须将嵌套资源放在具有问题类型和问题模型的路线中才能具有上述类型的链接,但我不知道如何像上面那样构建视图和分离。请帮助我或给我一个想法,更好的方法:(

4

1 回答 1

1

根据我对您问题的理解,这可能会解决您的问题:

在你的 app/views/questions/new.html.erb 你可以这样做:



    case params[:question_type_id]
    when 1
    render :partial=>"/questions/new_true_false_question"
    when 2
    render :partial=>"/questions/new_single"
    when 3
    render :partial=>"/questions/new_multi"
    end

然后按照 abow 或您喜欢的任何名称创建三个部分。

或者,您可以通过将渲染:部分更改为渲染:模板在控制器中执行此操作。

:)

于 2012-10-29T16:23:02.003 回答