嵌套属性和表单不保存Rails 4

时间:2016-10-11 03:03:44

标签: ruby-on-rails ruby-on-rails-4 nested-forms nested-attributes

我正在尝试将用户嵌套到帐户,并允许在创建帐户时创建用户。从同一表单创建帐户和用户时遇到问题,我的服务器输出对于尝试隔离问题不是很有帮助。

这是我尝试保存帐户和用户时的输出。

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"=>"swilson@ta#####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", "swilson@ta#####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

如果您需要更多信息或我遗漏了什么,请告诉我们!谢谢!

1 个答案:

答案 0 :(得分:0)

所以问题发生在--- title: "test" output: pdf_document --- ```{r fig.width=5} DT::datatable(head(iris, 20), width = '100%') ``` 。一旦我删除了那个匹配方法,一切都开始正常了。我会重做这个。

感谢。