使用Devise通过帐户模型Rails 5创建用户的问题

时间:2018-04-09 03:57:54

标签: ruby-on-rails

我正在努力实现的目标:

当用户向我的应用注册时,他们将被带到新的帐户创建页面。这是他们进入所需子域的地方。从这个表单我也想创建所有者(用户类)。

问题:

正如它现在所处,当我填写生成的表格(下方)

<%= form_for @account do |f| %>

<%= fields_for :owner do |o| %>
  <p>
  <%= o.label :f_name %>
  <%= o.text_field :f_name %>
</p>
  <p>
  <%= o.label :m_name %>
  <%= o.text_field :m_name %>
</p>
  <p>
  <%= o.label :l_name %>
  <%= o.text_field :l_name %>
</p>
  <p>
  <%= o.label :email %>
  <%= o.email_field :email %>
</p>
  <p>
  <%= o.label :password %>
  <%= o.password_field :password %>
</p>
  <p>
  <%= o.label :password_confirmation %>
  <%= o.password_field :password_confirmation %>
</p>

<% end %>

<p>
  <%= f.label :subdomain %>
  <%= f.text_field :subdomain %>
</p>
  <%= f.submit %>
<% end %>

并尝试提交表单,我得到以下rails服务器输出:

Started POST "/accounts" for 127.0.0.1 at 2018-04-08 21:52:57 -0600
Processing by AccountsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"4yUhk6N40udNBMoBJz/sFzbjC/RUtU7FVyHe9NlhtBkmpGEMZE0+xMcD7E6GLOjgp02hbkrbuMNLQ5gBjh+kvA==", "owner"=>{"f_name"=>"xxxxx", "m_name"=>"xxxxx", "l_name"=>"xxxxx", "email"=>"xxxxx@xxxxxnltd.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "account"=>{"subdomain"=>"testinga"}, "commit"=>"Create Account"}
   (0.2ms)  BEGIN
  Account Exists (0.6ms)  SELECT  1 AS one FROM "accounts" WHERE LOWER("accounts"."subdomain") = LOWER($1) LIMIT $2  [["subdomain", "testinga"], ["LIMIT", 1]]
   (0.1ms)  ROLLBACK
  Rendering accounts/new.html.erb within layouts/application
  Rendered accounts/new.html.erb within layouts/application (2.4ms)
Completed 200 OK in 49ms (Views: 21.5ms | ActiveRecord: 8.3ms)

现在,当我阅读输出时,我似乎无法找到为什么这会回滚而不是保存。我确实看到它告诉我一个帐户已存在于该子域名,但这是一个CLEAN数据库,并且没有保存帐户!当我在帐户控制器(下面)中的@ account.save之前运行byebug时,没有错误消息或我可以找到的详细信息。

我的AccountController :(我把控制器留在了控制器中,也许我把它放在了错误的地方?)

class AccountsController < ApplicationController

  def index
  end

  def show
  end

  def new
    @account = Account.new
    @account.build_owner
  end

  def create
    @account = Account.new(account_params)
    byebug
    if @account.save
      redirect_to root_path, notice: 'Account creates successfully.'
    else
      render action: 'new'
    end
  end

  def edit
  end

  def update
  end

  def destroy
  end

private

  def account_params
    params.require(:account).permit(:subdomain, :owner_id, :plan_id, :account_verified, :account_status, owner_attributes: [:id, :email, :password, :password_confirmation, :f_name, :m_name, :l_name, :office_country_code, :office_phone_number, :mobile_country_code, :mobile_phone_number])
  end

end

我的Account型号

class Account < ApplicationRecord

  RESTRICTED_SUBDOMAINS = %w(www admin loadlead)

  belongs_to :owner, class_name: 'User'
  has_many :users

  validates :owner, presence: true
  validates :subdomain, presence: true,
            #uniqueness: { case_sensitive: false },
            format: { with: /\A[\w\-]+\Z/i, message: 'contains invalid characters'},
            exclusion: { in: RESTRICTED_SUBDOMAINS, message: 'restricted name'}

  before_validation :downcase_subdomain

  accepts_nested_attributes_for :owner

protected

  def downcase_subdomain
    self.subdomain = subdomain.try(:downcase)
  end

end

我的User型号

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable

  belongs_to :account

end

非常感谢任何帮助!我不知道我哪里出错了?提前谢谢。

2 个答案:

答案 0 :(得分:2)

您正在调用不会引发异常的@account.save。如果一切正常,则返回true;如果验证失败,则返回false(如果@account.valid?返回false)。

如果有任何验证错误,您可以通过调用:

来检查它们
pry(main)> @account.valid?
pry(main)> false
pry(main)> @account.errors

这应该可以帮助您调试问题。

答案 1 :(得分:2)

尝试在fields_for构建器上调用f

<%= form_for @account do |f| %>
  <%= f.fields_for :owner do |o| %>
    <p>
      <%= o.label :f_name %>
      <%= o.text_field :f_name %>
    </p>

    # ....
  <% end %>

  # ....
  <%= f.submit %>
<% end %>

您可以删除:owner_id,当我们使用Rails时,:accepts_nested_attributes_for会自动设置此属性值。