views:

9

answers:

0

I'm saving nested objects within the objects they belong to, but when i do that they do not use the controller im saving but the parents controller.

class Project < ActiveRecord::Base
  belongs_to :company
  belongs_to :user
  has_many :tasks

  accepts_nested_attributes_for :tasks, :allow_destroy => true
end

in the views i have something like this

<% form_for @project do |c| %>
    <% c.fields_for :tasks,  @project.tasks.last do |p| %>
            <%= p.text_field :name %>
    <% end %>

    <%= submit_tag '+' %>
<% end %>

so what i'm trying to do, is update the user field with the fields for, that last field is specified in the controller.

  def show
    @project = Project.find(params[:id])
    @project.tasks.build
    @project.tasks.last.user = current_user # this should pass to the show.html.erb, to be saved back

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @project }
    end
  end

I'm thinking maybe the solution would be to check if the username is set in the nested objects, and if not to populate it with the current user in:

  def update
    @project = Project.find(params[:id])
    @project.user = current_user

    #find anything @project.....user blank and set to current user

    respond_to do |format|
      if @project.update_attributes(params[:project])
        format.html { redirect_to(@project, :notice => 'Project was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

I'm hoping that is the solution, and how do it do it?

an example of it running currently is at http://severe-fire-37.heroku.com