开发者

nested_form - edit action doesn't show nested elements

开发者 https://www.devze.com 2023-02-28 18:24 出处:网络
I have a nested_form where nested elements are dynamically added and removed in a form, using Javascript.

I have a nested_form where nested elements are dynamically added and removed in a form, using Javascript.

I've got this whole piece working for the new action - elements add and destroy, and everything save开发者_开发问答s as it should.

When accessing the edit action, I get the correct parent element but the child elements do not display - instead, the edit view teases me with a lingering (and unhelpful) 'Add Option' button.

Where might I be going wrong here?


try the gem cocoon it's really handy and takes care of the nested forms javascript for you!!

Here's the link http://www.dixis.com/?p=454&lang=nl


I had this exact same problem, and discovered that it was down to the "fields_for" block that created the nested elements. For instance, I had:

<%= nested_form_for(@recipe) do |f| %>

  # Stuff for parent...

  <% f.fields_for :recipe_ingredients do |ingredient_form| %>
    # Stuff for nested form...
  <% end %>
  # etc.

This line needed to be a <%= block, not a <% block as above, i.e.

<%= nested_form_for(@recipe) do |f| %>

  # Stuff for parent...

  # The following line has changed
  <%= f.fields_for :recipe_ingredients do |ingredient_form| %>
    # Stuff for nested form...
  <% end %>
  # etc.

That was all it needed to print out existing nested attributes on the edit form.


I had exactly the same issue (I seriously lost some hair on it). I was glad to find this question, no one else seemed to have it. My solution was "trivial": I upgraded from Rails 3.0.0 to 3.0.6 (3.0.7 brought up another issue) on my Windows Box.

I did not investigate any further, 'coz it works now. I did not change any single line of code. Perhaps this is a valid fix for ya, too.

Plz, let me know...

Regards, Dirk


Check out your form for editing the resource.
Try to use instance variable in parent form, like this:

<%= form_for @parent_resource do |form| %>
   ... some field ...
<%= form.fields_for :children_resources do |f| %>
...
<% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号