0

我有两个模型。一个公司和一个master_user,它是一个设计用户。尝试在公司控制器的创建操作中创建公司对象时,我得到一个未经许可的参数:master_user。

型号是

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

我允许每个 rails 4 要求的参数。但由于某种原因,它仍然拒绝我的 master_user

4

1 回答 1

0

问题是在 Rails 4 中使用强大的参数进行设计时,并不自动知道这些参数。所以只要明确地告诉他,就像在这个问题中一样。

于 2013-10-30T07:06:49.183 回答