我有一個表單,它是部分問卷調查表,如果選擇了某些答案,表單會顯示其他問題。我需要存儲這些問題的答案,並可能允許人們改變他們的答案,並且我更願意在一個地方做決定a)和b)在Rails中,所以我有一個表格來檢查我的@assessment
對象,並基於對象的狀態顯示或隱藏各種問題。JavaScript中的觸發軌跡控制器操作
我一直在想我如何使用AJAX設置往返旅行。我的表單處於不完整狀態,因此我可以在@assessment
對象更新時重新加載它。我見過的大多數示例都涉及到提交表單,並使用POST操作將params傳遞給Rails對象,然後使用JavaScript重新渲染部分內容。我試圖通過一個單選按鈕onClick,但不能解決我如何從那裏實際上得到一個新的更新的對象。
我該怎麼做?我應該努力找出一種更好的方式,這足以讓我感到痛苦嗎?
這裏的一個簡化形式部分,_form.html.haml:
= bootstrap_form_for @assessment, as: :put, url: project_assessment_path(@project), remote: :true do |f|
.panel.panel-default
.panel-heading
.panel-title Lorem ipsum dolor sit amet
.panel-body
= answer_question_1(@project)
- if show_question(@assessment, :question_2)
.panel.panel-default
.panel-heading
.panel-title Lorem ipsum dolor sit amet?
.panel-body
= f.radio_button :question_2, true, label: "Yes", inline: true, remote: :true
= f.radio_button :question_2, false, label: "No", inline: true, remote: :true
- if show_question(@assessment, :question_3)
.panel.panel-default
.panel-heading
.panel-title Lorem ipsum dolor sit amet?
.panel-body
= f.radio_button :question_3, true, label: "Yes", inline: true, remote: :true
= f.radio_button :question_3, false, label: "No", inline: true, remote: :true
...
- if @assessment.complete?
- case @assessment.assessment_message
- when :not_applicable
.panel.panel-success.hidden
.panel-body.alert-success
Duis scelerisque id ipsum et consectetur.
- when :low
.panel.panel-success.hidden
.panel-body.alert-success
Duis scelerisque id ipsum et consectetur.
- when :medium
.panel.panel-warning
.panel-body.alert-warning
Duis scelerisque id ipsum et consectetur.
- when :high
.panel.panel-info.hidden
.panel-body.alert-info
Additional information is required:
- if @assessment.form_required?
.panel.panel-success
.panel-heading
.panel-title Description of Work
.panel-body
= f.text_area :aims_hypothesis, label: "Aims/Hypothesis", rows: 4
= f.text_area :methodology, label: "Brief Description of Methodology", rows: 4
= f.text_area :significance, rows: 4
...
= f.form_group do
= f.submit "Save"
這被包含在這個div從edit.html.haml(因爲與在其上的到期日期自動創建@assessment,有沒有new
動作)
.row
#assessment-form.col-xs-12.col-md-11
= render partial: "form"
這裏的assessments_controller.rb的相關部分:
before_action :find_project
before_action :find_assessment
before_action :authorised?
def next_question
@assessment.update_attributes(strong_parameters)
respond_to do |format|
format :html { redirect_to sign_in_path }
format :js do
render partial: "update_assessments"
end
end
end
而且_update_assessments.js.coffee:
$('#assessment-form').html("<%= escape_javascript (render partial: 'form') %>")
請添加您的ajax調用JavaScript的問題。 – user100693
如何編寫AJAX調用是個問題。 – Merus