简体   繁体   English

动态创建新字段以将多个行条目提交到表模型中

[英]Dynamically create new fields to submit multiple row entries into the table model

I have a partial _form that contains multiple fields for creating a recipe. 我有一个_form,其中包含用于创建配方的多个字段。 I'm currently able to add a single entry for cooking step and the cooking step attaches itself with the recipe_id upon submission. 我目前能够添加一个条目烹饪步骤烹饪步骤重视本身在提交recipe_id。

Problem: 问题:

Obviously each recipe has MORE than just 1 cooking step. 显然,每个配方已经不仅仅是1烹调工序中更多 How do I dynamically add another/or more fields below it to create Step 2, Step 3, Step 4, and etc. All of these steps go into a model cooking_step.rb . 我如何在其下动态添加另一个/或更多字段以创建步骤2,步骤3,步骤4等。所有这些步骤都放入模型Cooking_step.rb中 I've seen the railscast video on nested forms and not only is it a bit confusing, I don't think it applies to what I'm doing since I'm working with the SAME MODEL as opposed to MULTIPLE MODELS. 我看过嵌套形式的Railscast视频,不仅有点令人困惑,而且我认为它不适用于我正在做的事情,因为我使用的是SAME MODEL ,而不是MULTIPLE MODELS。

Code: 码:

Below is what I have so far in my controller: 以下是我到目前为止在控制器中拥有的:

def create 
    @cookingstep = CookingStep.new(params[:cooking_step])
    @cookingstep.recipe_id = @recipe.id
    @cookingstep.save 
end

Below is what I have so far in my partial _form.html.erb: 以下是到目前为止我的_form.html.erb部分中的内容:

<%= fields_for @cookingstep do |din| %>
    <%= din.label :instruction %>
    <%= din.text_field :instruction %>
<% end %>

<%= submit_tag "Submit"  %>

A simpler design would be to have the steps 1, 2, 3, etc in a list format, with a blank space at the bottom for a new entry. 一种更简单的设计是将步骤1、2、3等以列表格式显示,并在底部留一个空白以用于新条目。 In the list, provide options for edit, delete, and change position in sequence. 在列表中,提供用于按顺序编辑,删除和更改位置的选项。 When you have that working you may want to give it some ajax. 工作完成后,您可能需要给它一些ajax。 Then the best ajax treatment for a recipe would be to make them draggable to change sequence, in_place_editing for in place editing, and a delete button next to each for deleting that specific step. 然后,对配方而言,最佳的ajax处理将是使其可拖动以更改顺序,in_place_editing用于就地编辑,并且紧挨着每个位置的删除按钮以删除该特定步骤。 I hope this helps. 我希望这有帮助。

您应该从具有许多烹饪步骤的食谱模型开始,然后按照轨道视频中的说明进行操作

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

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