简体   繁体   English

设置动态嵌套表单名称和ID属性

[英]set dynamic nested form name and ID attributes

I have a rails app with two models: Schedule and Task. 我有两个模型的Rails应用程序:计划和任务。 Task belongs_to Schedule and Schedule has_many Tasks. Task_to Schedule和Schedule has_many Tasks。 I use a nested form with javascript to allow the user to enter as many tasks as they want when creating a schedule. 我将嵌套表格与javascript结合使用,以允许用户在创建计划时输入所需的任意多个任务。 Here is my form: 这是我的表格:

<%= form_for [@project, @schedule] do |f| %>
    <%= f.fields_for :tasks do |builder| %>
        <%= render 'task_fields', :f => builder %>
    <% end %>
    <p><%= link_to_add_fields "Add task", f, :tasks %>
    <p><%= f.submit "Submit" %></p>
<% end %>

And here is the partial the form above refers to: 这是上面的表格所指的部分内容:

<p class = "fieldo">

     <%= f.label :title %><br />
     <%= f.text_field :title %><br />

     <%= f.label :content %><br />
     <%= f.text_field :content %><br />

     <%= f.hidden_field :_destroy %>

    <%= link_to_function "remove", "remove_fields(this)"  %>
</p>

Here is the schedules controller action for the form: 这是表单的Schedules Controller操作:

def new
    2.times {@schedule.tasks.build}
end

So, as you can see from the controller, the form will automatically print 2 task forms to the page. 因此,如您从控制器看到的那样,该表格将自动将2个任务表格打印到页面上。 Those 3 forms give the following html: 这3种形式提供以下html:

<p class = "fieldo">
    <input id="schedule_tasks_attributes_0_title" name="schedule[tasks_attributes][0][title]" type="text" /><br />
    <input id="schedule_tasks_attributes_0_content" name="schedule[tasks_attributes][0][content]" type="text" /><br />
</p>
<p class = "fieldo">
    <input id="schedule_tasks_attributes_1_title" name="schedule[tasks_attributes][1][title]" type="text" /><br />
     <input id="schedule_tasks_attributes_1_content" name="schedule[tasks_attributes][1][content]" type="text" /><br />
</p>

As you can see, the id and name start at 0 and increment by 1. This is good and is how i would expect it to work. 如您所见,id和名称从0开始,以1递增。这很好,这也是我期望它工作的方式。 The problem is that when i add a task dynamically by pressing the "Add task" button, the id and name become random numbers like this: 问题是,当我通过按“添加任务”按钮动态添加任务时,id和名称变成如下所示的随机数:

<p class = "fieldo">
    <input id="schedule_tasks_attributes_1387479041550_title" name="schedule[tasks_attributes][1387479041550][title]" type="text">
    <input id="schedule_tasks_attributes_1387479041550_content" name="schedule[tasks_attributes][1387479041550][content]" type="text">
</p>
<p class = "fieldo">
    <input id="schedule_tasks_attributes_1387479043642_title" name="schedule[tasks_attributes][1387479043642][title]" type="text">
    <input id="schedule_tasks_attributes_1387479043642_content" name="schedule[tasks_attributes][1387479043642][content]" type="text">
</p>

My questions are: 我的问题是:

  1. What are those numbers and how are they being generated? 这些数字是什么?它们是如何产生的?
  2. Is there any way to change this to get neater numbers without breaking the functionality? 有什么办法可以改变它以获得更整洁的数字而不破坏功能?

If anyone has seen rails cast #197, this form is based slightly on that. 如果有人看到过#197的导轨,则此表格略有依据。 Here is my javascript and a ruby helper I am also using. 这是我的JavaScript和我也在使用的ruby助手。

javascript: javascript:

//Dynamic forms
 function remove_fields(link) {
         $(link).prev("input[type=hidden]").val("1");
         $(link).closest(".fieldo").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)); 

 }

Application helper: 应用程序助手:

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, raw("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end

Without seeing more of your code, it's hard to be sure, but I think those are the "temporary" IDs created when you task.build on the rails side. 没有看到更多的代码,这很难确定,但是我认为这些是当您在rails端进行task.build时创建的“临时” ID。 Use debugger , pry , or some puts @schedule.tasks.all.collect{|t| t.id} 使用debuggerpry或一些puts @schedule.tasks.all.collect{|t| t.id} puts @schedule.tasks.all.collect{|t| t.id} to check this. puts @schedule.tasks.all.collect{|t| t.id}进行检查。

If that is the case, then no, you can't "get neater numbers", but you wouldn't want to - you just need those to be unique, and for Rails to handle creating the tasks nicely. 如果真是这样,那么不能,您不能“获得更整洁的数字”,但您不想这样做-您只需要它们是唯一的,并且让Rails很好地处理创建任务。 So, just check to see if your database has what you expect after the create action, and call it done. 因此,只需检查一下create动作后数据库是否具有期望的值,然后将其调用完成即可。

1): This huge number comes from the Javascript line var new_id = new Date().getTime(); 1):这个巨大的数字来自Java语言行var new_id = new Date().getTime(); it sets the HTML id of the new line with a timestamp, which makes it "almost" uniq. 它将带有时间戳的新行的HTML ID设置为“几乎”唯一。


2): Yes you could change this to use a "reasonnable human number". 2):是的,您可以将其更改为使用“合理的人员编号”。

function add_fields(link, association, content) {
         var new_id = Math.floor((Math.random()*10000)+3);
         var regexp = new RegExp("new_" + association, "g");
         $(link).parent().before(content.replace(regexp, new_id)); 
}

This would replace the huge number with a random number between 10003 and 3. But I recommend you to let Rails deal with this huge number: First, it discourages the potential evil users of you app, and Second, it is almost-for-sure a uniq id. 这将用一个介于10003和3之间的随机数代替这个巨大的数字。但是我建议您让Rails处理这个巨大的数字:首先,它阻止了您应用程序的潜在恶意用户,其次,这几乎是肯定的唯一ID。


But hey, always remember the #1 Developper's rule: 但是,请记住第一个开发人员的规则:

If it ain't broken, don't fix it. 如果没有损坏,请不要修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM