1

几天来,我一直在尝试向用户添加帐户。

我已经顺利安装了 Devise,但现在我想在我的注册表单中添加一个公司名称字段。

这是我的代码:

应用程序/模型/account.rb

class Account < ActiveRecord::Base
   attr_accessible :name
   has_many :users
   accepts_nested_attributes_for :users
end

应用程序/模型/user.rb

class User < ActiveRecord::Base

   belongs_to :account

   devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable

   attr_accessible :first_name, :last_name, :account_attributes, :account, :email, :password, :password_confirmation, :remember_me

end

app/views/devise/registrations/new.html.erb

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
   <%= devise_error_messages! %>

   <div><%= f.label :first_name %><br />
   <%= f.text_field :first_name %></div>

   <div><%= f.label :last_name %><br />
   <%= f.text_field :last_name %></div>

   <%= f.fields_for :account do |account_form| %>
      <div><%= account_form.label :name %><br />
  <%= account_form.text_field :name %></div>
   <% end %>

   <div><%= f.label :email %><br />
   <%= f.email_field :email %></div>

   <div><%= f.label :password %><br />
   <%= f.password_field :password %></div>

   <div><%= f.label :password_confirmation %><br />
   <%= f.password_field :password_confirmation %></div>

   <div><%= f.submit "Sign up" %></div>
<% end %>

这就是我拥有的所有代码。我没有再改变什么。

这是我得到的错误:

Account(#2175324980) expected, got ActiveSupport::HashWithIndifferentAccess(#2169631580)

提前致谢。

编辑

我找到了解决方案:

我变了

<%= f.fields_for :account do |account_form| %>

<%= f.fields_for :account_attributes, resource.account do |account_form| %>

现在我正在尝试向 account_form 添加一个 :plan ,我得到:

Can't mass-assign protected attributes: plan

编辑 2

好吧,菜鸟的错误。我刚刚将 :plan 添加到帐户模型中。

在那之后我遇到了一个查询错误,我只是将我的数据库中的一些字段从 null => false 更改为 null => true ,瞧。

4

1 回答 1

0

我希望它看起来像这样。这涉及子类化设计的注册控制器

#models
class Account < ActiveRecord::Base
   attr_accessible :name
   has_many :users
end

class User < ActiveRecord::Base
   belongs_to :account
   accepts_nested_attributes_for :account
   attr_accessible :first_name, :last_name, :account_attributes, :account, :email, :password, :password_confirmation, :remember_me

end

#app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
   def new
      @account = Account.new
      super
   end
end


#routes
devise_for :users, :controllers => {:registrations => "registrations"}


#view
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
   <%= devise_error_messages! %>

   <div><%= f.label :first_name %><br />
   <%= f.text_field :first_name %></div>

   <div><%= f.label :last_name %><br />
   <%= f.text_field :last_name %></div>

   <%= f.fields_for @account do |account_form| %>
      <div><%= account_form.label :name %><br />
  <%= account_form.text_field :name %></div>
   <% end %>

   <div><%= f.label :email %><br />
   <%= f.email_field :email %></div>

   <div><%= f.label :password %><br />
   <%= f.password_field :password %></div>

   <div><%= f.label :password_confirmation %><br />
   <%= f.password_field :password_confirmation %></div>

   <div><%= f.submit "Sign up" %></div>
<% end %>
于 2012-07-16T17:28:48.490 回答