2012-08-06 86 views
0

嗨,我想要做的嵌套形式,第1部分和第2部分的欄目。它似乎我有幾個問題,我不明白爲什麼。Javascript嵌套窗體Railcasts

Question 1: The add and remove links don't works, they show up but don't actually execute anything 
Question 2: I get the following error code and don't understand why? The error is regarding the f.error_messages is not recognized 
Question 3: When trying to create a surveys i get: Can't mass-assign protected attributes: answer 

謝謝你在這裏我的代碼相似railcasts 196,197

模型問題

class Question < ActiveRecord::Base 
    belongs_to :survey 
    attr_accessible :content, :question_id, :name, :answers_attributes 
    has_many :answers, :dependent => :destroy 
    accepts_nested_attributes_for :answers , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true 
end 

標準答案

class Answer < ActiveRecord::Base 
    belongs_to :question 
    attr_accessible :content, :question_id 
end 

示範調查

class Survey < ActiveRecord::Base 
    has_many :questions, :dependent => :destroy 
    accepts_nested_attributes_for :questions , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true 
    attr_accessible :name, :questions_attributes 
end 

視圖 形式

<%= form_for(@survey) do |f| %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <p><%= link_to_add_fields "Add Question", f, :questions %></p> 
    <%= f.fields_for :questions do |bf|%> 
    <% render 'question_fields', :f => bf %> 
    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

的question_fields

<p> 
    <%= f.label :content, "Question" %><br /> 
    <%= f.text_area :content, :rows=> 3 %> 
    <%= f.check_box :_destroy %> 
    <%= link_to_remove_fields "remove", f %> 
</p> 
<p><%= link_to_add_fields "Add Answer", f, :answers %></p> 
<%= f.fields_for :answer do |form| %> 
    <%= render 'answer_fields', :f => form %> 
<% end %> 

answer_fields

<p class="fields"> 
    <%= f.label :content, "Answer" %> 
    <%= f.text_field :content %> 
    <%= link_to_remove_fields "remove", f %> 
</p> 

控制器

的javascript 的application.js

function remove_fields(link) { 
    $(link).previous("input[type=hidden]").value = "1"; 
    $(link).up(".fields").hide(); 
} 

function add_fields(link, association, content) { 
    var new_id = new Date().getTime(); 
    var regexp = new RegExp("new_" + association, "g") 
    $(link).up().insert({ 
    before: content.replace(regexp, new_id) 
    }); 
} 

application_jquery.js

function remove_fields(link) { 
    $(link).prev("input[type=hidden]").val("1"); 
    $(link).closest(".fields").hide(); 
} 

function add_fields(link, association, content) { 
    var new_id = new Date().getTime(); 
    var regexp = new RegExp("new_" + association, "g") 
    $(link).parent().before(content.replace(regexp, new_id)); 
} 

任何幫助表示讚賞。

這裏的鏈接教程 http://railscasts.com/episodes/196-nested-model-form-part-1 http://railscasts.com/episodes/197-nested-model-form-part-2

而且路線不知道你的意思。

Surveysays::Application.routes.draw do 
    resources :surveys 
end 
+0

哪裏是你的路線?你也應該有嵌套的路線來支持'accepting_nested_attributes_for'的東西。另外,我相信我知道你指的是哪些情節,但是你能將鏈接發佈到你正在關注的劇集中嗎? – jefflunt 2012-08-06 15:25:04

+0

Accepts_nested_attributes_for在我的模型中,是不是應該在哪裏? – Jseb 2012-08-06 15:27:24

+0

沒關係,對不起。我重新閱讀代碼,我想我可能是錯誤的嵌套路線。 – jefflunt 2012-08-06 15:46:55

回答

0

問題1:按下按鈕時不會調用這些函數。在railscast中,他將link_to_remove和link_to_add添加到應用程序助手。

module ApplicationHelper 
    def link_to_remove_fields(name, f) 
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") 
    end 

def link_to_add_fields(name, f, association) 
    new_object = f.object.class.reflect_on_association(association).klass.new 
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| 
     render(association.to_s.singularize + "_fields", :f => builder) 
    end 
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")") 
    end 
end 

問題2:不知道,但它可能被清除了一些其他的修復

問題3:你有一個類型的位置:

<%= f.fields_for :answer do |form| %> 
    <%= render 'answer_fields', :f => form %> 
<% end %> 

應該

<%= f.fields_for :answers do |form| %> 
    <%= render 'answer_fields', :f => form %> 
<% end %> 

這是答案,因爲你有一個has_many關係船。屬性答案不存在,默認情況下也列入白名單。這就是爲什麼你得到質量分配錯誤。

也有這樣一個簡單的版本在他修改過的插曲:

http://railscasts.com/episodes/196-nested-model-form-revised

+0

嗯,我應該從修改或更新將工作? – Jseb 2012-08-06 16:12:48

+0

要麼工作。修改後的情節只是稍微清理了一些東西,但對相同的概念進行了研究。但是,沒有修改的版本是有效的。你只是有幾個錯別字,沒辦法調用你的JavaScript函數。您可能遇到的另一個問題是您已經定義了兩次javascript函數。你只需要定義一次。 – Joeyjoejoejr 2012-08-06 16:19:24

+0

我該如何申報? – Jseb 2012-08-06 16:29:48