0
등록 할 때 몇 가지 질문에 대답해야하는 사용자가 있습니다. 사용자는 Answers join 테이블을 통해 많은 질문을합니다. 난 그냥 사용자 # 새로운 작업에 양식을 만드는 방법을 알아 내려고 노력하고 있어요. 나는 simple_form을 사용하고있다.Ruby on Rails has_many 관계에 대한 양식
ActiveRecord::Schema.define(:version => 20120831144008) do
create_table "answers", :force => true do |t|
t.integer "user_id"
t.integer "question_id"
t.text "response"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "answers", ["user_id", "question_id"], :name => "index_answers_on_user_id_and_question_id"
create_table "questions", :force => true do |t|
t.string "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
# 새 페이지, 나는 질문 제목을 통해 반복하고있는 사용자에 따라서
, 그리고 각 질문 아래의 텍스트 영역을 생성하고이 만들어 취득이 필요합니다 : 여기 내 DB 스키마입니다. 여기에 내가 지금까지 양식에 대해 가지고있는 것은 작동하지 않으며 아마 해결책을 위해 많은 도움이되지 않을 것이다. 이 절대 올바른 방법이지만 완전히 추한 아니라면
<%= simple_form_for(@user) do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :phone %>
<%= f.input :organization %>
<%= f.input :primary_contact %>
<%= f.association :answers, collection: @questions %>
<% @questions.each do |question| %>
<div>
<strong><%= question.title %></strong>
<%= text_field_tag 'questions[]' %>
</div>
<% end %>
<%= f.button :submit %>
<% end %>
솔루션 나는이 확실하지, 작업을 얻을 수 있었다. 내 컨트롤러에서 나는 @user.answers.build
질문을 통해 내 simple_form의 I 루프를 가지고 채워 필요한 데이터와 응답 필드를 만들
<% Question.all.each do |question| %>
<%= f.simple_fields_for :answers do |a| %>
<div>
<strong>
<%= question.title %>
</strong>
<%= a.hidden_field :question_id, value: question.id %>
<%= a.input :response, label: false %>
</div>
<% end %>
<% end %>
이 논리를 실제로 잘 따라갈 수 없었습니다. 나는 작업 솔루션으로 내 질문을 업데이트했지만, 꽤 좋지는 않을 수도 있습니다. 답장을 보내 주셔서 감사합니다. – Lsdafjklsd