Javascript 代码仅生成 HTML 打印输出,无法呈现它

发布于 2024-12-04 07:24:16 字数 2630 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文