Javascript 代码仅生成 HTML 打印输出,无法呈现它
我是 RoR 的新手,正在关注这个 railscast解释如何在嵌套表单中删除和添加元素。就我而言,我试图允许用户从购物“列表”中添加/删除“项目”。然而不幸的是,当我在浏览器中单击“添加项目”时,HTML 被打印出来,而不是呈现。它的输出如下:
<div class ="fields"> Quantity: <input id="list_items_attributes_1315942810959_quantity" name="list[items_attributes][1315942810959][quantity]" size="7" type="text" /> Name: <input id="list_items_attributes_1315942810959_name" name="list[items_attributes][1315942810959][name]" size="30" type="text" /> Category: <select id="list_items_attributes_1315942810959_category" name="list[items_attributes][1315942810959][category]"><option value="Produce">Produce</option> <option value="Dairy">Dairy</option></select> <p><input id="list_items_attributes_1315942810959__destroy" name="list[items_attributes][1315942810959][_destroy]" type="hidden" value="false" /><a href="#" onclick="remove_fields(this); return false;">remove</a></p> </div>
Add Item
我觉得我错过了一些简单的东西。我可以打开一些开关来渲染它吗?谢谢!
如果它有帮助,这是我的列表模型:
class List < ActiveRecord::Base
has_many :items, :dependent => :destroy
accepts_nested_attributes_for :items, :reject_if => lambda { |a| a[:name].blank?}, :allow_destroy => true
belongs_to :user
end
这是我将项目添加到列表的简短形式:
<%= form_for @list do |f| %>
<p>
<%= f.label :date %>
<%= f.date_select :date %>
</p>
<%= f.fields_for :items do |builder|%>
<%= render "item_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add Item", f, :items %></p>
<%= submit_tag "Create List" %>
<% end %>
这是我的应用程序帮助程序中的 link_to_add_fields 方法(它是来自 Railscast 的逐字副本):
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
最后,这是我的应用程序中的 add_fields 函数。 js 文件:
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)
});
}
I'm a newbie to RoR and am following this railscast that explains how to remove and add elements within nested forms. In my case, I'm trying to allow users to add/remove "items" from a shopping "list." Unfortunately, however, when I click "add item" in my browser, HTML is printed out, instead of rendered. Here's what it outputs:
<div class ="fields"> Quantity: <input id="list_items_attributes_1315942810959_quantity" name="list[items_attributes][1315942810959][quantity]" size="7" type="text" /> Name: <input id="list_items_attributes_1315942810959_name" name="list[items_attributes][1315942810959][name]" size="30" type="text" /> Category: <select id="list_items_attributes_1315942810959_category" name="list[items_attributes][1315942810959][category]"><option value="Produce">Produce</option> <option value="Dairy">Dairy</option></select> <p><input id="list_items_attributes_1315942810959__destroy" name="list[items_attributes][1315942810959][_destroy]" type="hidden" value="false" /><a href="#" onclick="remove_fields(this); return false;">remove</a></p> </div>
Add Item
I feel like I'm missing something simple. Is there some switch that I can turn on to get this to render? Thanks!
In case it's helpful, here's my list model:
class List < ActiveRecord::Base
has_many :items, :dependent => :destroy
accepts_nested_attributes_for :items, :reject_if => lambda { |a| a[:name].blank?}, :allow_destroy => true
belongs_to :user
end
Here's my short form for adding items to a list:
<%= form_for @list do |f| %>
<p>
<%= f.label :date %>
<%= f.date_select :date %>
</p>
<%= f.fields_for :items do |builder|%>
<%= render "item_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add Item", f, :items %></p>
<%= submit_tag "Create List" %>
<% end %>
Here's my link_to_add_fields method in my application helper (it's a verbatim copy from the railscast):
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
And finally, here's my add_fields function in my application.js file:
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)
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论