Last active
August 24, 2017 01:04
-
-
Save staycreativedesign/4f481d2c96be2de44374d7b4a791073e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Questionnaire < ApplicationRecord | |
has_many :questions | |
has_many :member_questionnaires | |
has_many :users, through: :member_questionnaires | |
accepts_nested_attributes_for :question | |
end | |
class Question < ApplicationRecord | |
belongs_to :questionnaire | |
has_one :answer | |
end | |
class Answer < ApplicationRecord | |
belongs_to :question | |
end | |
%h1 | |
questionnaires#show | |
= form_for @questionnaire do |f| | |
= @questionnaire.questions.each do |question| | |
%h1 | |
= question.description | |
- fields_for :question, question.create_answer do |answer| | |
= answer.text_field :description | |
#what im trying to do... iterate over the questions and then have a text_field for the answers | |
# show the question.description and below that show a answer text_field ( the user cannot edit questions ) | |
resources :questionnaires, only: [:index, :edit, :update] do | |
resources :questions, only: [] do | |
resources :answers, only: [:edit, :update, :create, :new], shallow: true | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before(:each) do | |
@user = create_fake_member | |
q = create(:questionnaire) | |
4.times do | |
create(:question, questionnaire: q) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few things concern me about this line:
question.create_answer
is going to create a new answer in the database every time a user loads the page, not just when the form is submitted. I think what you want isquestion.build_answer
.fields_for
should be called on the form variablef
or whateverfields_for
fields variable it's nested under._fields
to thefields_for
block variable names will help distinguish model instances from field builders.:question
, but then you name the variableanswer
. These should match. I think instead you want a separatefields_for
for each level of russian dolls:Maybe that'll get you closer… this is all off the top of my head, untested. For further reading and understanding, these should be helpful:
fields_for
docs