Javascript嵌套表格Railcasts

时间:2012-08-06 15:18:23

标签: ruby-on-rails ruby ruby-on-rails-3

您好我正在尝试使用嵌套表格,第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-1 http://railscasts.com/episodes/197-nested-model-form-part-2

路线不确定你的意思。

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

1 个答案:

答案 0 :(得分: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