Skip to content

Instantly share code, notes, and snippets.

@thanhcuong1990
Created July 25, 2014 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thanhcuong1990/2976549bd117517a1b54 to your computer and use it in GitHub Desktop.
Save thanhcuong1990/2976549bd117517a1b54 to your computer and use it in GitHub Desktop.
Nested attributes example 2
# /app/models/user.rb
class User < ActiveRecord::Base
has_one :account_setting, :dependent => :destroy
accepts_nested_attributes_for :account_setting
end
# /app/controllers/users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new(:account_setting => AccountSetting.new)
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to(@user, :notice => 'User was successfully created.')
else
render :action => "new"
end
end
end
# /app/views/users/edit.html.erb
<%= form_for(@user) do |f| %>
# ...
<%= f.fields_for :account_setting do |a| %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment