Created
May 18, 2011 17:16
-
-
Save jonhoman/979043 to your computer and use it in GitHub Desktop.
fields_for in a has_many relationship works, but not with a has_one relationship
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
class User < ActiveRecord::Base | |
has_many :profiles | |
accepts_nested_attributes_for :profiles | |
end | |
# app/models/profile.rb | |
class Profile < ActiveRecord::Base | |
belongs_to :user | |
end | |
# app/controllers/users_controller.rb | |
class UsersController < ApplicationController | |
def new | |
@user = User.new | |
@user.profiles.build | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @user } | |
end | |
end | |
end | |
# app/views/users/_form.html.erb | |
<%= form_for(@user) do |f| %> | |
<div class="field"> | |
<%= f.label :name %><br /> | |
<%= f.text_field :name %> | |
</div> | |
<%= f.fields_for :profiles do |p| %> | |
<div class="field"> | |
<%= p.label :status %><br /> | |
<%= p.text_field :status %> | |
</div> | |
<% end %> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> | |
# This works just fine. Yay! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
class User < ActiveRecord::Base | |
has_one :profile | |
accepts_nested_attributes_for :profile | |
end | |
# app/models/profile.rb | |
class Profile < ActiveRecord::Base | |
belongs_to :user | |
end | |
# app/controller/users_controller.rb | |
def new | |
@user = User.new | |
@user.build_profile | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @user } | |
end | |
end | |
# app/views/users/_form.html.erb | |
<%= form_for(@user) do |f| %> | |
<div class="field"> | |
<%= f.label :name %><br /> | |
<%= f.text_field :name %> | |
</div> | |
<%= f.fields_for(:profile) do |p| %> | |
<div class="field"> | |
<%= p.label :status %><br /> | |
<%= p.text_field :status %> | |
</div> | |
<% end %> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> | |
# FAIL! Doesn't associate the user and profile. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
class User < ActiveRecord::Base | |
belongs_to :profile | |
accepts_nested_attributes_for :profile | |
end | |
# app/models/profile.rb | |
class Profile < ActiveRecord::Base | |
has_one :user | |
end | |
# app/controllers/users_controller.rb | |
class UsersController < ApplicationController | |
def new | |
@user = User.new | |
@user.build_profile | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @user } | |
end | |
end | |
end | |
# app/views/users/_form.html.erb | |
<%= form_for(@user) do |f| %> | |
<div class="field"> | |
<%= f.label :name %><br /> | |
<%= f.text_field :name %> | |
</div> | |
<%= f.fields_for :profile do |p| %> | |
<div class="field"> | |
<%= p.label :status %><br /> | |
<%= p.text_field :status %> | |
</div> | |
<% end %> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> | |
# This also works, but feels backwards. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment