简体   繁体   中英

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. I'm currently able to add a single entry for cooking step and the cooking step attaches itself with the recipe_id upon submission.

Problem:

Obviously each recipe has MORE than just 1 cooking step. 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 . 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.

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:

<%= 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. 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. 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. I hope this helps.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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