ruby - Rails 4 Create record with record.id of join table -


Ruby on Rail 4

I am trying to create a form in my test table. Near the record should be included near the ID (S), which is named as Question_Test. Do I need to create a record in the questions of questions from questions and then create a record record? How would you do this?

Model (Not sure I have enrolled in the right table):

  class test < ActiveRecord :: Base has has_many: questions_tests has_many: Question, via = & gt; : Question_tests and class questions & lt; ActiveRecord :: Base has has_many: questions_tests has_many: tests, via = & gt; : Quiz questionnaire question quiz & lt; ActiveRecord :: Base is_to: question is_to: test end  

My schema:

  create_table "questions", force: true do | T | T.string "content" t.string "question_type" t.string "category" t.integer "product_id" t.boolean "active" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" end add_index Using the "question", ["product_id", "created_at"], name: "index_questions_on_product_id_and_created_at", :: btree create_table "questions_tests", id: false, force: true do | T | T.integer "test_id", blank: incorrect t.integer "question_id", empty: wrong end create_table "test", force: true do | T | T.string "name" t.integer "user_id" t.string "type" t.string "category" t.string "description" end  

form should be filled in test attributes and Either way the IDs from question_tests are I do not have a question of how to send or create a record of questions.

My form, it is not certain how to select questions and how to store them for a test record. Here,: question_id is undefined but I need to store 2 to 200 stores in them.

  & lt; H1 & gt; New Test & lt; / H1> & Lt;% = form_for @test do F | | & Gt%; & Lt;% = f.label: Name, "Test Name"%> & Lt; Br> & Lt;% = f.text_field: Name, Category: "Input-LG"%> & Lt;% = f.label: Details, "Description"% & gt; & Lt; Br> & Lt;% = f.text_field: Description, Category: "Input-LG"%> & Lt;% = f.label: question_id% & gt; & Lt; Br> & Lt;% = f.select: question_id, question. All.collect {| P | | [P.content, p.id]}, category: "Input-LG"%> & Lt;% = f.label: Category% & gt; & Lt; Br> & Lt;% = f.select: Category, [["IP Voice Telephony", "IP_WOISE"], ["IP Video Monitoring", "IP_Video_VirVillance"], ["IP Video Telephony", "IP_Video_Telephony"], [" "Prompt Gateways", "Enterprise_Gateways", "[Consumer ATA", "ConsumerTat"], ["IP PBX", "IP_PBX"]], {prompt: "Select category"}, category: "Input-LG"% & Gt; & Lt;% = f.label: Type% & gt; & Lt; Br> & Lt;% = f.select: type, [["beginning", "beginning"], ["intermediate", "intermediate"], ["advanced", "advanced"]], {prompt: "select type" }, Class: "Input-LG"% & gt; & Lt; Br / & gt; & Lt; Br / & gt; & Lt; Br / & gt; & Lt;% = f.submit "Create Test", Category: "BTN BTN-LG BTN-Primary"% & gt; & Lt;% end% & gt;  

You have to consider fields_for .

You have

fields_for blocks in your form

  f.fields_for: questions do | Questions | questions. Selection (...) End ' 

Within this block, you will have a way to select questions to add to the test. A gem is said to help you add a "Add Question" link to add more questions.

You have to add to: nested_attributes_for: questions in your test model. Then the railway has made the question area , you do not have to worry about it.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

lua - HowTo create a fuel bar -