1

I have the model Box, and each Box has many box_videos (another model). I want the user to be able to add box_videos to the box, so I created the following edit form for that (after creation of @box):

<%= form_tag "/box_videos", { method: :post, id: "new_box_videos", remote: true } do %>
    <%= text_field_tag "box_videos[][link]", '' %>
    <%= text_area_tag "box_videos[][description]", '' %>
    <%= hidden_field_tag("box_videos[][box_id]", @box.id) %>
    <%= hidden_field_tag("box_videos[][user_id]", current_user.id) %>

    <div class="another_video">Add Another Video</div>
    <%= submit_tag "Save Videos" %>
<% end %>

<%= form_for(@box) do |f| %>
    <%= f.text_field :name %>
    <%= f.text_field :size %>
    <%= f.text_field :all_other_attributes %>

    <%= f.submit "Create Box" %>
<% end %>

And some Javascript to facilitate adding more box_videos in one click.

<script>
$('.another_video').click(function() {
  $('#new_box_videos').prepend('<input id="box_videos_link" name="box_videos[][link]" placeholder="Link to a youtube video." style="width: 18em;" type="text" value=""><textarea id="box_videos_description" name="box_videos[][description]" placeholder="Describe this video." style="width: 18em;"></textarea><br/><br/><input id="box_videos_box_id" name="box_videos[][box_id]" type="hidden" value="' + gon.box_id.toString() + '"><input id="box_videos_user_id" name="box_videos[][user_id]" type="hidden" value="' + gon.user_id.toString() + '">');
});
</script>

The above code works, in that params[:box_videos] when submitting three box_videos is as follows:

[{"link"=>"https://www.youtube.com/watch?feature=player_detailpage&v=dpAP8bq3ddU
", "description"=>"foo", "box_id"=>"63", "user_id"=>"16"}, {"link"=>"https
://www.youtube.com/watch?feature=player_detailpage&v=dpAP8bq3ddU", "description"
=>"bar", "box_id"=>"63", "user_id"=>"16"}, {"link"=>"https://www.you
tube.com/watch?feature=player_detailpage&v=dpAP8bq3ddU", "description"=>"hello world",
"box_id"=>"63", "user_id"=>"16"}]

In my controller I would just create a box_video object for every hash in the array and it works out just fine. BUT the problem comes when each time I submit the nested form_tag form, I send multiple requests to the controller action! Which means there are duplicates being created.

I can think of adding logic to the box_videos controller create action to check for duplicate content, but it seems rather hacky. Can anyone please let me know why this is happening?

jytoronto
  • 560
  • 3
  • 9
  • 18
  • This is because every time you submit the form, it's sending _everything_ over and over again. Honestly, I think nested forms is really the way to go. There are a bunch of tutorials online on how to do that in an elegant way. – Hesham Feb 03 '14 at 15:35
  • yes that is the observed the behavior, but any thoughts on why everything is sent over and over again? – jytoronto Feb 03 '14 at 16:00

1 Answers1

0

You can't have nested form elements according to the html spec (see this answer).

You might want to use nested forms for this, it provides the creation of associated models via jquery with useful form_helper wrappers.

Community
  • 1
  • 1
Fletcher91
  • 384
  • 5
  • 11