Nested with polymorphic patterns

I am creating an application with the following attributes, and I am working on creating a single form in order to be able to save the goal, tasks, main stages and important tasks.

#app/models/goal.rb has_many :tasks, :as => :achievement has_many :milestones accepts_nested_attributes_for :tasks accepts_nested_attributes_for :milestones #app/models/milestone.rb belongs_to :goal has_many :tasks, :as => :achievement #app/models/task.rb belongs_to :achievement, :polymorphic => true 

Whenever I save a goal with its attributes, it seems that the task models are confused as to which attribute of reachability they belong to, as a result of which each important task is simply displayed as a goal. Below are my forms, partial codes and controller code.

the form:

 <%= 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 %> 

milestone_fields partial:

  <%= f.label :content, "Milestone" %> <%= f.text_field :content %> <%= f.fields_for :tasks do |ff| %> <%= render 'shared/task_fields', :f => ff %> <% end %> <%= f.link_to_remove "Remove milestone" %> <%= f.link_to_add "Add Milestone Task", :tasks %> 

task_fields partial:

  <%= f.label :content, "Task" %> <%= f.text_field :content %> <%= f.link_to_remove "Remove Task" %> 

Goal controller:

 def new @goal = current_user.goals.new end def create @user = current_user @goal = @user.goals.build(params[:goal]) if @goal.save flash[:success] = "Goal created!" redirect_to goals_path else render 'new' end end 

I tried adding @ goal.milestones.build and @ goal.tasks.build next to the f.fields_for code, which seems to have fixed it, but leads to other problems such as an empty edit form (no pre -populated data), and also milestone areas and tasks that appear immediately, instead of clicking a link to open a blank field. If you cannot solve it, are there any sites where I can pay other encoders to help solve a small problem like this? I'm desperate.

+4
source share
1 answer

Your Milestone model needs the following line to be able to accept tasks_attributes:

 # app/models/milestone.rb accepts_nested_attributes_for :tasks 

Everything else in the models looks good. As for the fields_for form command, passing build will create a new instance of the specified object, which will lead to the creation of an empty editing form that you describe. Try removing these assemblies if nested_form_for handles creating and adding these new form fields.

I recreated your environment, and when I submit my form, I get a hash of the parameters in the server log:

 {"goal"=>{"milestones_attributes"=>{"0"=>{"content"=>"foo", "tasks_attributes"=>{"0"=>{"content"=>"bar"}}}}, "tasks_attributes"=>{"0"=>{"content"=>"baz"}}}} 

This created two Records of the Assignment, one of the goals to achieve the goal, and the other to achieve the Milestone

Regarding paying someone for this work, http://www.rent-acoder.com/ is a website, I heard people say that they accepted the job in the past. I can’t talk about the quality of the code, but you can publish your work and see what bites you get.

+2
source

All Articles