アカウントに1つの請求先住所を持つことを許可するアカウント管理システムを作成しようとしています。私はアカウントとアドレスが独自のコントローラとモデルを持つことを望んでいます。管理者ユーザーは新しいアカウントを作成し、このアカウントの新しい請求先住所を作成するためにリダイレクトされます。最初の部分は機能し、私はアカウントを作成することができますが、2番目はaccount_id属性をアドレステーブルに保存するだけです。以下は私のコードです:has_oneの関係を実装できません
accounts_controller
class AccountsController < ApplicationController
def new
@account = Account.new
end
def create
@account = Account.new(params[:account])
@account.build_address
if @account.save
flash[:success] = "Customer Account has been successfully created!"
redirect_to '/newaddress'
else
render 'new'
end
end
end
addresses_controller
class AddressesController < ApplicationController
def new
@address = Address.new
end
def create
@account = Account.find(params[:account])
@address = @account.create_address(params[:address])
redirect_to root_path
end
end
account_model
class Account < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
attr_accessible :contactFirstName, :contactLastName, :contactEmail, :contactPhone, :business_name
end
address_model
class Address < ActiveRecord::Base
attr_accessible :city, :state, :street, :zipCode
belongs_to :account
end
私はまた、以下に、最終
resources :accounts do
resources :addresses
end
私のroutes.rbをファイルに次が(私はプログラミングやレールにまだ新しいですので、私は投稿relavantテーブル
create_table "accounts", :force => true do |t|
t.string "contactEmail"
t.string "contactFirstName"
t.string "contactLastName"
t.string "contactPhone"
t.datetime "joinDate"
t.string "business_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "addresses", :force => true do |t|
t.string "city"
t.string "state"
t.string "street"
t.string "zipCode"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
end
のスキーマです追加
追加新しいアカウントページを開き、新しいアドレスページに移動したときのログを追加しました。 [:アカウント]
Started GET "/newaccount" for 127.0.0.1 at Tue Sep 20 17:21:56 -0500 2011
Processing by AccountsController#new as HTML
Rendered shared/_error_messages.html.erb (0.6ms)
Rendered accounts/_account_fields.html.erb (13.0ms)
Rendered layouts/_stylesheets.html.erb (1.2ms)
User Load (2.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
Rendered layouts/_header.html.erb (19.4ms)
Rendered layouts/_footer.html.erb (1.0ms)
Rendered accounts/new.html.erb within layouts/application (50.3ms)
Completed 200 OK in 168ms (Views: 64.6ms | ActiveRecord: 2.5ms)
Started POST "/accounts" for 127.0.0.1 at Tue Sep 20 17:22:11 -0500 2011
Processing by AccountsController#create as HTML
Parameters: {"commit"=>"Next", "account"=>{"business_name"=>"FooBar", "contactLastName"=>"Bar", "contactPhone"=>"1231231234", "contactEmail"=>"[email protected]", "contactFirstName"=>"Foo"}, "authenticity_token"=>"lJG89TIjcJighmFUWLg1uR9sJq0CHLvceeLH9QNocGY=", "utf8"=>"✓"}
SQL (0.1ms) BEGIN
SQL (0.3ms) SELECT 1 FROM `accounts` WHERE (LOWER(`accounts`.`contactEmail`) = LOWER('[email protected]')) LIMIT 1
SQL (1.2ms) describe `accounts`
AREL (0.4ms) INSERT INTO `accounts` (`created_at`, `contactFirstName`, `business_name`, `contactPhone`, `updated_at`, `contactEmail`, `contactLastName`, `joinDate`) VALUES ('2011-09-20 22:22:11', 'Foo', 'FooBar', '1231231234', '2011-09-20 22:22:11', '[email protected]', 'Bar', NULL)
SQL (1.5ms) describe `addresses`
AREL (0.2ms) INSERT INTO `addresses` (`zipCode`, `state`, `city`, `updated_at`, `account_id`, `street`, `created_at`) VALUES (NULL, NULL, NULL, '2011-09-20 22:22:11', 31, NULL, '2011-09-20 22:22:11')
SQL (26.3ms) COMMIT
Redirected to http://localhost:3500/newaddress
Completed 302 Found in 85ms
Started GET "/newaddress" for 127.0.0.1 at Tue Sep 20 17:22:11 -0500 2011
Processing by AddressesController#new as HTML
Rendered addresses/_address_fields.html.erb (83.4ms)
Rendered layouts/_stylesheets.html.erb (1.4ms)
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
Rendered layouts/_header.html.erb (17.1ms)
Rendered layouts/_footer.html.erb (0.9ms)
Rendered addresses/new.html.erb within layouts/application (113.6ms)
Completed 200 OK in 126ms (Views: 119.6ms | ActiveRecord: 30.3ms)
Started POST "/newaddress" for 127.0.0.1 at Tue Sep 20 17:22:29 -0500 2011
Processing by AddressesController#new as HTML
Parameters: {"address"=>{"city"=>"Boston", "street"=>"123 Main St", "zipCode"=>"02222", "state"=>"MA"}, "commit"=>"Create", "authenticity_token"=>"lJG89TIjcJighmFUWLg1uR9sJq0CHLvceeLH9QNocGY=", "utf8"=>"✓"}
Rendered addresses/_address_fields.html.erb (10.3ms)
Rendered layouts/_stylesheets.html.erb (1.2ms)
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
Rendered layouts/_header.html.erb (17.2ms)
Rendered layouts/_footer.html.erb (0.9ms)
Rendered addresses/new.html.erb within layouts/application (40.1ms)
Completed 200 OK in 54ms (Views: 47.3ms | ActiveRecord: 0.2ms)
私はそれが仕事を得ることができます。これにより、私は自分のページにそれぞれのフォームを持っているときに、正しいaccount_idを渡すのではなく、アドレステーブルにどこに書き込むかを知っていると思います。 –
私はfields_forを使用し、必要に応じて視覚的にAjaxと分離します(例えば、ステップ1、ステップ2などのようにする必要がある場合など) – jschorr