4

我正在使用RB railcast 的设置 - #197 Nested Model Form Part 2来动态地将字段添加到表单中,但我在让Tokeninput 字段工作时遇到问题。

形式:

<%= form_for(current_user, :url => user_products_path) do |f| %>
  <%= f.error_messages %>
    <%= f.fields_for :user_products do |builder| %>
        <%= render "user_product_fields", :f => builder %>
    <% end %>
    <%= link_to_add_fields "Add a UserProduct", f, :user_products %>
    <%= f.submit "Create" %>
<% end %>

这是我的 user_product_fields 部分,令牌 text_fields 是我遇到的问题:

<div class="fields">
        <%= f.label :product_token, "Product" %>
        <%= f.text_field :product_token, :id => 'product_token' %>
        <%= f.label :address_token, "Address" %>
        <%= f.text_field :address_token, :id => 'address_token' %>
        <%= f.label :price %>
        <%= f.text_field :price %>
        <%= link_to_remove_fields "remove", f %>
</div>

我的 application.js 中的 Jquery Tokeninput 函数:

$(function() {
  $("#product_token").tokenInput("/products.json", {
    prePopulate: $("#product_token").data("pre"),
    tokenLimit: 1
  });
});

$(function() {
  $("#address_token").tokenInput("/business_addresses.json", {
    prePopulate: $("#address_token").data("pre"),
    tokenLimit: 1
  });
});

嵌套形式在函数中的作用是:

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));
}

function remove_fields(link) {
    $(link).prev("input[type=hidden]").val("1");
    $(link).closest(".fields").hide();
}

这一行在这里:

var new_id = new Date().getTime();

使 tokeninput 字段动态化,这是我从 HTML 中提取的,请注意字段中不断变化的长数字。这是因为上面的行。

<label for="user_user_products_attributes_1313593151076_product_token">Product</label>
<label for="user_user_products_attributes_1313593146876_product_token">Product</label>
<label for="user_user_products_attributes_1313593146180_product_token">Product</label>

当字段不断变化时,如何让我的令牌字段工作?

谢谢你。


编辑:新的工作代码。

 function add_fields(link, association, content) {
    var new_id = new Date().getTime();
    var regexp = new RegExp("new_" + association, "g")
    $(content.replace(regexp, new_id)).insertBefore($(link).parent()).trigger("nestedForm:added");
}

$('div.fields').live("nestedForm:added", function() {
  $("#product_token", $(this)).tokenInput("/products.json", {
    prePopulate: $("#product_token", $(this)).data("pre"),
    tokenLimit: 1
  });
});

尝试data-pre使用 TokenInput 时:

def new
  @user_product = current_user.user_products.build

  # This line below is for TokenInput to work, This allowed me to use @products.map on the form.
  @products = []
end

def edit
  @user_product = UserProduct.find(params[:id])

  # This line below had to find the Product associated with the UserProduct
  @products = [@user_product.product]
end
4

1 回答 1

2

You can user jQuery's insertBefore instead of before as that will return the inserted element. It will help you to trigger some event. You can have a listener on this event, in which you can have you token-input code. You should also use class instead of id, as many elements can have same class, but no two elements should have same id.

$(content.replace(regexp, new_id)).insertBefore($(link).parent()).trigger("nestedForm:added");

$('div.fields').live("nestedForm:added", function() {
  $(".product_token", $(this)).tokenInput("/products.json", {
    prePopulate: $(".product_token", $(this)).data("pre"),
    tokenLimit: 1
  });
});

This is just an idea, code is not tested.

于 2011-08-17T17:26:23.190 回答