Created
July 25, 2014 17:07
-
-
Save thanhcuong1990/2976549bd117517a1b54 to your computer and use it in GitHub Desktop.
Nested attributes example 2
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 :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