0

我想將用戶嵌套到一個帳戶,並允許創建帳戶時創建用戶。從同一表單創建帳戶和用戶時,我遇到了問題,而我的服務器輸出對嘗試隔離問題沒有太大幫助。嵌套屬性和窗體不保存Rails 4

這裏是我嘗試保存帳號和用戶時的輸出。

Started POST "/accounts" for ::1 at 2016-10-10 20:55:23 -0600 
Processing by AccountsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"UK7h4edNzRomi7JGomoZD1ayYmlNxI/h2ZH+NEaJxWQzcFsYAJujr5EDDS2HeprAX41IuS5/crRxmXYz80YpYw==", "account"=>{"subdomain"=>"mydomain", "owner_attributes"=>{"email"=>"[email protected]#####td.com", "f_name"=>"S####", "l_name"=>"W####", "date_of_birth"=>"19##-##-##", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}, "commit"=>"Create Account"} 
    (0.3ms) BEGIN 
    User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "[email protected]#####td.com"], ["LIMIT", 1]] 
    Account Exists (0.3ms) SELECT 1 AS one FROM "accounts" WHERE "accounts"."subdomain" IS NULL LIMIT $1 [["LIMIT", 1]] 
    (0.2ms) ROLLBACK 
    Rendering accounts/new.html.erb within layouts/application 
    Rendered accounts/_form.html.erb (3.8ms) 
    Rendered accounts/new.html.erb within layouts/application (5.1ms) 
    Rendered shared/_signed_out_nav.html.erb (1.3ms) 
Completed 200 OK in 183ms (Views: 44.2ms | ActiveRecord: 1.1ms) 

我認爲這個問題可能與帳戶生成和子域有關。但我不能把它釘在我的生命中!

也是我註釋掉我用####個人信息...

這裏是我的帳戶控制器

class AccountsController < ApplicationController 

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

    def create 
    @account = Account.new(account_params) 
    respond_to do |format| 
     if @account.save 
     format.html {redirect_to root_path, notice: 'Account successfully created.'} 
     else 
     format.html {render :new, alert: 'There was a problem, please try again.'} 
     end 
    end 
    end 

    private 

    def account_params 
    params.require(:account).permit(:subdomain, owner_attributes: [:email, :password, :password_confirmation, :f_name, :l_name, :date_of_birth]) 
    end 

    def set_account 
    @account = Account.find(params[:id]) 
    end 

end 

這裏是我的賬戶形式:

<%= bootstrap_form_for(@account) do |f| %> 
    <div class="row"> 
    <div class="col-xs-12"> 
     <%= f.text_field :subdomain, hide_label: true, placeholder: 'Company Name', append: ".patrolvault.net" %> 
    </div> 
    </div> 

    <%= f.fields_for :owner do |o| %> 
    <div class="row"> 
     <div class="col-xs-12"> 
     <%= o.email_field :email, label: 'Email Address' %> 
     </div> 
     <div class="col-xs-12"> 
     <%= o.text_field :f_name, label: 'First Name' %> 
     </div> 
     <div class="col-xs-12"> 
     <%= o.text_field :l_name, label: 'Last Name' %> 
     </div> 
     <div class="col-xs-12"> 
     <%= o.date_field :date_of_birth, label: 'Date Of Birth' %> 
     </div> 
     <div class="col-xs-6"> 
     <%= o.password_field :password, label: 'Password' %> 
     </div> 
     <div class="col-xs-6"> 
     <%= o.password_field :password_confirmation, label: 'Confirm Password' %> 
     </div> 
    </div> 
    <% end %> 

    <%= f.submit :class => 'btn btn-primary' %> 
<% end %> 

和這裏是我的賬戶型號:

class Account < ApplicationRecord 
    # Before Actions 
    before_validation :downcase_subdomain 

    # Relationships 
    belongs_to :owner, class_name: 'User', optional: true 
    accepts_nested_attributes_for :owner 

    # Validations 
    validates :owner, presence: true 

    RESTRICTED_SUBDOMAINS = %w(www, patrolvault, test) 
    validates :subdomain, presence: true, 
         uniqueness: { case_sensitive: false }, 
         format: { with: /\A[\w\-]+\Z/i, message: 'Contains invalid characters.' }, 
         exclusion: { in: RESTRICTED_SUBDOMAINS, message: 'Restricted domain name'} 

    # Custom Methods 

    private 

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

end 

如果您需要更多信息,或者我錯過了一些東西,請讓我知道!謝謝!

回答

0

所以問題出在我的before_validation :downcase_subdomain。一旦我刪除了這個和匹配的方法,一切都開始正常工作。我會重做這個。

謝謝。