Reputation: 8624
I'm built app which allow users create questions are type: True/False, Single and Multiple Choice. So, I have created some models:
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
Now i would to build an index page of questions, have 3 link to add question for 3 type of question, when users click a link, they will go to page to create question and page will have appropriate form for type of that question. In Question controller, i want to store question type id to save to question.
I think the addresses like this:
http://example.com/questions/index : Index page will have 3 link to create question.
http://example.com/question_types/1/questions/new: Will render partial form for True/False question
http://example.com/question_types/2/questions/new: Will render partial form for Single choice question
http://example.com/question_types/1/questions/new: Will render partial form for Multiple choice question
I think i have to put nested resources in my routes with question type and question model to have above type of link, but i don't know how to build view and separate like above. please help me or give me an idea, better way to do it :(
Upvotes: 1
Views: 461
Reputation: 1599
By my understanding of your question, this might solve your question:
in your app/views/questions/new.html.erb you can do it like this:
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
and then create the three partials as refered abow or in whatever names you like.
OR you can do it in the controller by chaning render :partial to render :template.
:)
Upvotes: 1