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
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.