我正在關注嵌套表單上的this railscast以創建類似的應用程序。更改表單多個部分
在瑞安的應用程序,他有如下的結構:
- 調查---> 2.問題---> 3.答案
所以用戶可以創建一個調查,並添加/刪除問題,並提供可添加/刪除的相應答案。
我試圖做一點不同。我有:
- 調查---> 2.問題---> 3.答案或評論
我的應用程序,用戶已經創建了一個問題之後,用戶可以創建一個答案(就像Ryan的應用程序一樣)。但每個答案旁都有一個選擇框,默認爲「答案」,另一個爲「評論」。我試圖獲得Ryan的應用程序基本相同的功能,但有兩個表:評論表和答案表。
我的代碼模仿railscast,除了我answer_fields部分看起來像:
<fieldset>
<%= f.label :content, "Answer" %>
<%= f.select :switch_answer_table options_for_select(["Answer", "Comment"]), class: "change-type" %>
<%= f.text_field :content %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "remove" %>
</fieldset>
,我有一個comment_fields部分看起來像:
<fieldset>
<%= f.label :content, "Comment" %>
<%= f.select :switch_answer_table options_for_select(["Comment", "Answer"]), class: "change-type" %>
<%= f.text_field :comment_content %>
... (leaving out some stuff) ...
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "remove" %>
</fieldset>
我question.rb模式是這樣的:
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers
has_many :comments
accepts_nested_attributes_for :answers, allow_destroy: true
accepts_nested_attributes_for :comments, allow_destroy: true
end
我一直在努力解決這個問題,而且我被卡住了。我的jQuery不僅看起來像這樣至今:
jQuery ->
$('form').on 'click', '.remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('fieldset').hide()
event.preventDefault()
$('form').on 'click', '.add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
$('form').on 'change', '.change-type', (event) ->
# Confused on how to change partials
基本上,我需要一種方法來的onChange,部分所選答案/評論。
你不能使導軌後的部分進行處理,unluss你做阿賈克斯。如果不是這種情況,請創建兩個表單,隱藏一個表單並在select的change事件中管理它們的隱藏表示。 – lcguida 2014-09-29 17:31:55