2 models in one form - how to save the data to respective tables?

I have this action for updating data:

  def edit
    @project = Project.find(params[:id])
    if @project.team
      @team = Team.find(@project.id)
    else
      @team = Team.new
    end
  end

Form:

= form_for @project do |f|
  ...
  = f.fields_for @team do |t|
  #if I use following: = f.fields_for :team do |t|, then the form inputs in this form are not displayed
    ...
  end

end

Models:

class Project < ActiveRecord::Base
  has_one :team
  accepts_nested_attributes_for :team
end

class Team < ActiveRecord::Base
  belongs_to :project
end

When I try to send the form, I get following error message

Team(#2501295380) expected, got ActiveSupport::HashWithIndifferentAccess(#2157764620)

I found similar posts here on SO, but no one helped me to solved this issue, that's why I'll be very grateful for every advice.

Many thanks

Answers


This is solving my issue:

  def edit
    @project = Project.find(params[:id])
    unless @project.team.nil?
      @project.team
    else
      @project.build_team
    end  
  end

Need Your Help

how to do things between two UIAlertView (ios7)

ios objective-c ios7 uialertview

I define a UIAlertView, its tag = 101, to determine save or not, show another UIAlertView called alertView2 when clicked save button, and then delete the subviews of rootView. But when I call clear...

Flash Error #1009: Cannot access a property or method of a null object reference

flash object reference null

I've been stuck with such an error for 2 days now, and however I search on solving this from the number of previous questions asked about this error, I still can't seem to get it to work, mainly be...