Railscast 问题 #197 - 嵌套模型形式第 2 部分
我正在尝试在具有问题、答案和(多项选择)选项的系统中实现 Ryan 的 Railscast #197。 http://railscasts.com/episodes/197-nested-model-表单-第 2 部分。
- 我已经成功地实现了这些表单/部分之间的嵌套。
- 删除记录的更简单的“复选框”方式可以正常工作。
- 当我尝试添加/删除记录时出现问题。
我已经完全按照他的 Railscast 中出现的方式复制了代码:
#new.html.erb
<%= javascript_include_tag :defaults, :cache => true %>
<% f.fields_for :in_options do |builder| %>
<%= render "option_fields", :f => builder %>
<% end %>
#_option_fields.html.erb partial
<%= f.hidden_field :_destroy %>
<%= link_to_function "remove", "remove_fields(this)" %>
#application_helper.rb (exact same as #197)
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, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end
#application.js (exact same as #197. I have an Event.addbehavior below this code.)
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)
});
}
2 个问题:
- 当我单击“删除”链接时,它不会删除 - 它只是将页面向上或向下移动。
- 当我包含 link_to_add_fields "Add Answer", f, :answers 时,我得到 nil:NilClass 的未定义方法“klass”。
------进展------
如果我将函数remove_fields(link) 移动到new.html.erb 的顶部,则删除链接有效。这意味着我在访问 application.js 中的函数时遇到问题。这是我的精简结构。
# layout forms.html.erb
<html>
<head>
<%= stylesheet_link_tag "global", "forms", "candidateCreateProfile", "LiveValidation", "formsAccount", :cache => true %>
<%= javascript_include_tag :defaults, "LiveValidation" %>
</head>
<body>
<%= yield %>
</body>
</html>
# new.html.erb
<%= stylesheet_link_tag "interviewQuestions" %>
<%= javascript_include_tag "effects", "lowpro", "toggle", :cache => true %>
...#everything else, including my call to remove_fields
I'm trying to implement Ryan's Railscast #197 in a system with Questions, Answers, and (multiple choice) Options. http://railscasts.com/episodes/197-nested-model-form-part-2.
- I have successfully implemented the nesting among these forms/partials.
- The simpler 'check box' way to delete records works properly.
- The problem occurs when I try to add/delete records.
I have copied the code exactly as it appears in his Railscast:
#new.html.erb
<%= javascript_include_tag :defaults, :cache => true %>
<% f.fields_for :in_options do |builder| %>
<%= render "option_fields", :f => builder %>
<% end %>
#_option_fields.html.erb partial
<%= f.hidden_field :_destroy %>
<%= link_to_function "remove", "remove_fields(this)" %>
#application_helper.rb (exact same as #197)
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, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end
#application.js (exact same as #197. I have an Event.addbehavior below this code.)
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)
});
}
2 problems:
- When I click on the 'remove' link it doesn't remove - it just shifts the page up or down.
- When I include link_to_add_fields "Add Answer", f, :answers, I get undefined method `klass' for nil:NilClass.
------PROGRESS------
If I move function remove_fields(link) to the top of new.html.erb, the remove link works. Which means, I have a problem accessing the function in my application.js. Here's my condensed structure.
# layout forms.html.erb
<html>
<head>
<%= stylesheet_link_tag "global", "forms", "candidateCreateProfile", "LiveValidation", "formsAccount", :cache => true %>
<%= javascript_include_tag :defaults, "LiveValidation" %>
</head>
<body>
<%= yield %>
</body>
</html>
# new.html.erb
<%= stylesheet_link_tag "interviewQuestions" %>
<%= javascript_include_tag "effects", "lowpro", "toggle", :cache => true %>
...#everything else, including my call to remove_fields
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于那些仍然有 nil:NilClass 未定义方法“klass”问题的人,请参阅以下线程 - has_many :通过nested_form可以构建多个实例
For those who still have undefined method `klass' for nil:NilClass issue see the following thread — has_many :through nested_form that can build multiple instances
只是偶然发现了这个问题 - 如果有帮助,请使用 Ryan Bates 的这个很棒的 嵌套表单 gem。
这是 Railscast。
Just stumpled upon this question - if it helps, use this awesome nested form gem by Ryan Bates.
Here's the railscast.
您是否确定 app...js 包含在输出中?您可能需要编辑您的布局。
Have you made sure app...js is being included in the output? You may need to edit your layout.
在我的例子中,javascript没有被调用,我将请求更改为:
In my case the javascript wasn´t called so, I change the request for:
您正在使用原型库语法而不是 jQuery。访问剧集页面,查找提供了原型和 jQuery 语法的代码使用部分。
You are using a prototype library syntax instead of jQuery. Visit the episode page, and look for the code used part where both prototype and jQuery syntaxes are provided.