我构建的应用程序,允许用户创建问题的类型:真/假,单一和多个选择。因此,我创建了一些模型:
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:将为多项选择题呈现部分形式
我想我必须把我的问题类型和问题模型在我的路线嵌套资源,以有上述类型的链接,但我不知道如何建立视图和分离像上面。请帮助我或给我一个想法,更好的方法:
发布于 2012-10-30 00:23:02
根据我对你问题的理解,这可能会解决你的问题:
在你的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然后创建下面提到的三个分音,或者用你喜欢的任何名称。
或者,您可以在控制器中通过将render :partial更改为render :template来执行此操作。
:)
https://stackoverflow.com/questions/13123268
复制相似问题