无法实现 has_one 关系

发布于 2024-12-05 13:16:24 字数 5679 浏览 0 评论 0原文

我正在尝试创建一个帐户管理系统,允许一个帐户拥有一个帐单地址。我希望帐户和地址有自己的控制器和模型。管理员用户将创建一个新帐户,然后被重定向以为此帐户创建新的帐单地址。第一部分有效,我能够创建帐户,但第二部分仅将 account_id 属性保存到地址表中。下面是我的代码:

accounts_controlleraddresses_controlleraccount_modeladdress_model

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

陌生,所以我发布了

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

(我对编程和rails仍然

class Account < ActiveRecord::Base
  has_one :address
  accepts_nested_attributes_for :address
  attr_accessible :contactFirstName, :contactLastName, :contactEmail, :contactPhone, :business_name
end

我还在我的routes.rb

class Address < ActiveRecord::Base
  attr_accessible :city, :state, :street, :zipCode   
  belongs_to :account
end

文件中添加了以下内容

resources :accounts do
  resources :addresses
end

最后,下面是相关表的架构

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)

I am trying to create an account management system that allows an account to have one billing address. I want the account and address to have their own controller and model. An admin user would create a new account then be redirected to create a new billing address for this account. The first part works, I am able to create the account, but the second only saves the account_id attribute to the addresses table. Below is my code:

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

I also added the following to my routes.rb file

resources :accounts do
  resources :addresses
end

Last, below is the schema for the relavant tables

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

(I am still new to programming and rails so I posted as much as I thought would be relevant.)

Added Log from when I open the new account page and then proceed to the new address page.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-12-12 13:16:24

地址类中的行对我来说看起来是错误的(取出 _attributes):

class Address < ActiveRecord::Base
    attr_accessible :city, :state, :street,  :zipCode   
    belongs_to :account
 end

The line in in the Address class looks wrong to me (take out the _attributes):

class Address < ActiveRecord::Base
    attr_accessible :city, :state, :street,  :zipCode   
    belongs_to :account
 end
や莫失莫忘 2024-12-12 13:16:24

快速查看一下,您仅传递 params[:account]、尾部日志并查看您需要的内容

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

,您添加了日志,您现在可以看到,当您发布 newaddress 时

 Parameters: {"address"=>{"city"=>"Boston", "street"=>"123 Main St", "zipCode"=>"02222", "state"=>"MA"}, "commit"=>"Create", "authenticity_token"=>"lJG89TIjcJighmFUWLg1uR9sJq0CHLvceeLH9QNocGY=", "utf8"=>"✓"}

,您的控制器已

   def create
@account = Account.find(params[:account])
@address = @account.create_address(params[:address])
redirect_to root_path

结束

正如您所看到的,没有 @account = Account.find(params[:account])

因此您的帐户为空,您可以添加带有 account_id 的隐藏字段来形成或捕获取决于您的应用程序的其他方式。

As quick look, you are passing only params[:account], tail log and see what you need

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

Ass you added log, you can see now that when you post newaddress

 Parameters: {"address"=>{"city"=>"Boston", "street"=>"123 Main St", "zipCode"=>"02222", "state"=>"MA"}, "commit"=>"Create", "authenticity_token"=>"lJG89TIjcJighmFUWLg1uR9sJq0CHLvceeLH9QNocGY=", "utf8"=>"✓"}

and your controller have

   def create
@account = Account.find(params[:account])
@address = @account.create_address(params[:address])
redirect_to root_path

end

As you can see there are no @account = Account.find(params[:account])

So you account is null, you may add hidden field with account_id to form or catch other way depends of of your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文