![](/img/trans.png)
[英]Dynamically hide/show radio buttons in rails form doesn't hide when unchecked
[英]Handling radio buttons when applying to a nested form created dynamically
我有一個應用程序, users
可以在其中創建自己的forms
,然后將其應用於自己的clients
forms
是動態創建的,這意味着用戶可以添加他們想要的盡可能多的questions
,並且每個問題都有很多choices
(例如調查結構)。
到目前為止,到目前為止,我在應用表格時一直在掙扎,尤其是在保存answers
保留answers
並在呈現edit
視圖時正確顯示answers
。
呈現edit
視圖時,每個問題的選擇都將乘以Answered_form有多少個問題/答案(我不知道確切是哪個,我在這里猜測)。
Answers
會保留在數據庫中,盡管每個問題的選擇都會成倍增加,但選中的答案會被檢查並在原始答案的集合中被選中。 (回答前三個選擇中的一個,第二三個選擇中的兩個,第三三個選擇中的三個,第四三個選擇中的四個)
我已經在SO中閱讀了2個類似的問題, RoR的嵌套屬性在edit時會產生重復 ,而嵌套的表單更新操作會產生重復的結果 ,但是我已經在強參數中使用了:id
(您可以在下面的代碼中看到它)。
我想在這里澄清一下: 使用嵌套的問題和嵌套的選項創建表單工作得很好,並且還可以編輯創建的表單。 困難在於何時在應用程序中使用,回答或應用它 。
碼:
_form.html.erb:
<%= form_for [@child, @answered_form] do |f| %>
<%= f.hidden_field :form_id %>
<%= f.hidden_field :child_id %>
<div class="answered_form">
<h1><%= @form.f_title %></h1>
<h3><%= @form.f_description %></h3>
<br>
<% questions = @form.questions %>
<% i = 1 %>
<% questions.each do |question| %>
<%= i.to_s + ". " %><%= question.q_title %>
<br />
<% choices = question.choices %>
<%= f.fields_for :answers do |a| %>
<% choices.each do |choice| %>
<%= a.radio_button :a_content, choice.c_description %>
<%= a.label :a_content, choice.c_description, :value => choice.c_description, class: 'no-margin' %>
<br />
<% end %>
<% end %>
<br />
<% i += 1 %>
<% end %>
</div>
<div class="text-center">
<%= f.submit yield(:button_text), class: "btn btn-primary btn-lg" %>
</div>
<% end %>
answer_forms_controller.rb:
class AnsweredFormsController < ApplicationController
before_action :correct_answered_form, only: [:edit, :update, :destroy]
def new
@child = current_user.children.find(params[:child_id])
@form = current_user.forms.find(params[:form_id])
@answered_form = @child.answered_forms.new(form_id: params[:form_id])
@answered_form.answers.build
end
def create
@answered_form = AnsweredForm.create(answered_form_params)
if @answered_form.save
flash[:success] = "New survey " + @answered_form.form.f_title + " applied to patient!"
redirect_to current_user.children.find(params[:child_id])
else
render 'new'
end
end
def edit
@child = current_user.children.find(params[:child_id])
@form = current_user.forms.find(params[:form_id])
end
def update
if @answered_form.update_attributes(answered_form_params)
flash[:success] = "Survey updated!"
redirect_to @answered_form.child
else
render 'edit'
end
end
def show
end
def destroy
@child = current_user.children.find(params[:child_id])
@form = current_user.forms.find(params[:form_id])
@answered_form.destroy
redirect_to :back
end
private
# Strong params for creating and updating forms
def answered_form_params
params.require(:answered_form).permit(:form_id, :child_id, answers_attributes: [:id, :a_content, :a_boolean, :_destroy, :choice_id])
end
# Confirms the correct answered_form
def correct_answered_form
@answered_form = AnsweredForm.find(params[:id])
end
end
日志: POST:
Started POST "/children/1-juan-gomez-pereira/answered_forms" for ::1 at 2016-07-08 11:55:01 -0400
Processing by AnsweredFormsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hFrRfEwVG4XSkdbPwrohm1QEQ0FtE/as3sM2Fj3Av3reVHJxZBVKPeuAeD713H7gVyZn7eppnULDhLJQz+EBeg==", "answered_form"=>{"form_id"=>"1", "child_id"=>"1", "answers_attributes"=>{"0"=>{"a_content"=>"Bajo"}, "1"=>{"a_content"=>"Sí"}, "2"=>{"a_content"=>"Derecha"}, "3"=>{"a_content"=>"Pesado"}}}, "commit"=>"Aplicar", "child_id"=>"1-juan-gomez-pereira"}
補丁:
Started PATCH "/children/1-juan-gomez-pereira/answered_forms/3" for ::1 at 2016-07-08 11:55:54 -0400
Processing by AnsweredFormsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oGdqgUR95HMkMff40Gq1zWar/CH64F0jsN3oRHT1q/H6acmMbH21yx0gWQnnDOq2ZYnYjX2aNs2tmmwChtQV8Q==", "answered_form"=>{"form_id"=>"1", "child_id"=>"1", "answers_attributes"=>{"0"=>{"a_content"=>"Bajo", "id"=>"9"}, "1"=>{"a_content"=>"Bajo", "id"=>"10"}, "2"=>{"a_content"=>"Bajo", "id"=>"11"}, "3"=>{"a_content"=>"Confuso", "id"=>"12"}, "4"=>{"a_content"=>"No", "id"=>"9"}, "5"=>{"a_content"=>"Sí", "id"=>"10"}, "6"=>{"a_content"=>"Confuso", "id"=>"11"}, "7"=>{"a_content"=>"Confuso", "id"=>"12"}, "8"=>{"a_content"=>"Confuso", "id"=>"9"}, "9"=>{"a_content"=>"Izquierda", "id"=>"10"}, "10"=>{"a_content"=>"Confuso", "id"=>"11"}, "11"=>{"a_content"=>"Izquierda", "id"=>"12"}, "12"=>{"a_content"=>"Liviano", "id"=>"9"}, "13"=>{"a_content"=>"Liviano", "id"=>"10"}, "14"=>{"a_content"=>"Pesado", "id"=>"11"}, "15"=>{"a_content"=>"Liviano", "id"=>"12"}}}, "commit"=>"Actualizar", "child_id"=>"1-juan-gomez-pereira", "id"=>"3"}
我注意到在POST時,答案的id
沒有作為參數傳遞。 如果我檢查控制台,則可以看到答案已正確創建。
我認為這可能是問題所在
<% choices.each do |choice| %>
如果您已經構建了question.answers
,則只能使用f.fields_for :answers
對其進行迭代,並跳過上面的行。 我認為choices
與answers
沒有太大不同。
如果我錯了,請告訴我。
所以問題出在這行代碼:
<%= f.fields_for :answers do |a| %>
fields_for輔助函數會為您要為其調用的對象的整個集合生成字段。
在這種情況下, @answered_form
創建后有4個answers
,因此在編輯中調用fields_for時,它將為每個問題生成4個字段。
為了解決這個問題,您需要指定一個適合您需求的集合,並將其通過fields_for助手:
<%= f.fields_for :answers, ANSWERS_COLLECTION do |a| %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.