0
我有兩種模式。公司和作爲設計用戶的master_user。在公司控制器的創建操作中嘗試創建公司對象時,我收到一個不允許的參數:master_user。使用accept_nested_attributes_for從視圖中節省兩個模型無法保存
模型是
company.rb
class Company < ActiveRecord::Base
has_many :users
has_one :master_user
accepts_nested_attributes_for :master_user
end
master_user.rb
class MasterUser < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :company
end
這裏是我公司視
<%= form_for(@company) do |f| %>
<% if @company.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@company.errors.count, "error") %> prohibited this company from being saved:</h2>
<ul>
<% @company.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for @company.master_user do |m_user| %>
<div class="field">
<%= m_user.label :email %>
<%= m_user.text_field :email %>
</div>
<div class="field">
<%= m_user.label :password %>
<%= m_user.password_field :password %>
</div>
<div class="field">
<%= m_user.label :confirm_password %>
<%= m_user.password_field :confirm_password %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
和公司控制
# GET /companies/new
def new
@company = Company.new
@company.build_master_user
end
# POST /companies
# POST /companies.json
def create
puts "a"
@company = Company.new(company_params)
puts "b"
respond_to do |format|
if @company.save
format.html { redirect_to @company, notice: 'Company was successfully created.' }
format.json { render action: 'show', status: :created, location: @company }
else
puts "c"
format.html { render action: 'new' }
format.json { render json: @company.errors, status: :unprocessable_entity }
end
end
end
def company_params
params.require(:company).permit(:id, :name, :isTrial, :employMax, master_user_attributes: [:id, :email, :password, :confirm_password, :company_id])
end
我允許每個軌道參數4的要求。但由於某種原因,它仍然拒絕我的master_user