开发者

Nested attribute not saving in a form with polymorphic associations

开发者 https://www.devze.com 2023-03-19 19:23 出处:网络
I\'m trying to make an app where a user can save goals, milestones for those goals, tasks for the milestones, and tasks for the goal itself. I\'m using polymorphic associations, but making a form to i

I'm trying to make an app where a user can save goals, milestones for those goals, tasks for the milestones, and tasks for the goal itself. I'm using polymorphic associations, but making a form to input all of them has proven difficult. The problem is that the milestones aren't saving at all, and the milestone tasks are being listed in the database as having type "Goal" instead of type "Milestone" The models and database are set up like the top answer for this question. I'm hoping someone could take a look at my form_for implementation and see if it's correct, or if the problem is somewhere else. Let me know if you need to see some other code.

<%= nested_form_for @goal do |f| %>

  <%= render 'shared/error_messages', :object => f.object %>
  <%= render 开发者_运维百科'shared/goal_fields', :f => f %>

  <%= f.fields_for :milestones do |ff| %>
    <%= render 'shared/milestone_fields', :f => ff %>
  <% end %>  

  <%= f.fields_for :tasks do |ff| %>
    <%= render 'shared/task_fields', :f => ff %>
  <% end %>

  <%= f.link_to_add "Add Milestone", :milestones %>
  <%= f.link_to_add "Add Task", :tasks %>
  <%= f.submit %>

<% end %>


The Rails form builder method fields_for allows you to nest attributes for multiple records. This part of your code looks correct (assuming that your partials are working). You can make your fields_for line more explicit by building the relationship off of the goal object as follows:

<%= f.fields_for :milestones, @goal.milestones.build do |ff| %>
  <%= render 'shared/milestone_fields', :f => ff %>
<% end %>

Ensure that your models have the following code in order to process the parameters that will be passed to each of these models:

# app/models/goal.rb
 has_many :milestones
 has_many :tasks
 accepts_nested_attributes_for :milestones
 accepts_nested_attributes_for :tasks

# app/models/milestone.rb
 has_many :tasks
 accepts_nested_attributes_for :tasks # For tasks on milestones

Also ensure that if you are using attr_accessible to lock down your model attributes from mass-assignment, that these entries have corresponding entries (milestones_attributes, tasks_attributes, etc)

When you submit the form, look at the rails development log, and ensure that you see the parameters come through in a format similar to:

{:goal => {:milestones_attributes => {:tasks_attributes => {}, :tasks_attributes => {} }}

If all this data is going through, but the record is still not being saved, check for "ROLLBACK" entries in the log that might indicate that a record is not valid, and could not be inserted.

More info on nested_attributes can be found here: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Info on the form helpers utilizing these nested attributes can be found here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

0

精彩评论

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