こんにちは私はレールキャストのネストされたフォーム、パート1とパート2を実行しようとしています。いくつかの問題が発生しているようですが、その理由がわかりません。
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
レールキャスト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-1http://railscasts.com/episodes/197-nested-model-form-part-2
そして、ルートはあなたが何を意味するのかわからない。
Surveysays::Application.routes.draw do
resources :surveys
end