无法保存嵌套属性

Can not save nested attributes

这是表单视图:

<% provide(:title, 'Create Brokerage') %>
<%= render partial: 'shared/errors', :locals => {:object => @brokerage}%>
<h1>Create User</h1>
<%= form_for @brokerage, url: {action: "create_brokerage"} do |f|  %>
<%= f.fields_for :user do |builder| %>
    <%= render 'users/form_elements', f: builder %>
    <% end %>
<h1>Create Broker</h1>
    <div class="form-group">
      <%= f.label :about, class: 'sr-only'  %>
      <%= f.text_area :about, rows: 7, class: 'form-control', placeholder: 'About' %>
    </div>
    <div class="form-group">
      <%= f.label :phone, class: 'sr-only'  %>
      <%= f.text_field :phone, class: 'form-control', placeholder: 'Phone Number' %>
    </div>
    <div class="form-group">
      <%= f.label :email, class: 'sr-only'  %>
      <%= f.text_field :email, class: 'form-control', placeholder: 'Email Address' %>
    </div>
    <div class="form-group">
      <%= f.label :image, 'Upload Image', class: 'btn btn-primary trigger'  %>
      <%= f.file_field :image, style: 'display:none;', class: 'uploadBtn' %>
    </div>

<div class="form-group">
  <%= f.submit 'Create Brokerage', class: 'btn btn-primary' %>
</div>

<% end %>

users/_form_elements.html.erb

<div class="form-group">
  <%= f.label :first_name, class: 'sr-only' %>
  <%= f.text_field :first_name, class: 'form-control top-input', placeholder: 'First Name' %>
  <%= f.label :last_name, class: 'sr-only' %>
  <%= f.text_field :last_name, class: 'form-control bottom-input', placeholder: 'Last Name'%>
</div>
<div class="form-group">
  <%= f.label :email, class: 'sr-only'  %>
  <%= f.text_field :email, class: 'form-control', placeholder: 'Email Address' %>
</div>
<div class="form-group">
  <%= f.label :password, class: 'sr-only'  %>
  <%= f.password_field :password, class: 'form-control password-form top-input', placeholder: 'Password' %>

  <%= f.label :password_confirmation, 'Confirmation', class: 'sr-only'  %>
  <%= f.password_field :password_confirmation, class: 'form-control password-form bottom-input', placeholder: 'Confirm Password' %>
</div>

这是创建操作:

def create_brokerage
  @brokerage = Brokerage.new(brokerage_params)
  @brokerage.first_name = params[:brokerage][:user][:first_name]
  @brokerage.last_name = params[:brokerage][:user][:last_name]
  clean_phone
  if @brokerage.save
    redirect_to login_path, notice: 'Broker was saved.'
  else
    render 'brokerages/signup'
  end
end

private
def brokerage_params
  params.require(:brokerage).permit(:first_name, :last_name, :about, :email, :phone, :image, :user => [:id, :first_name, :last_name, :email, :password, :password_confirmation])
end

brokerage.rb

has_many :users, through: :brokers
accepts_nested_attributes_for :users

broker.rb

class Broker < ActiveRecord::Base
  belongs_to :brokerage
  belongs_to :user
end

user.rb

has_many :brokerages, through: :brokers

我保存的时候,服务器输出:

Started POST "/brokerages/create_brokerage" for 127.0.0.1 at 2015-09-02 23:24:07 -0700
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by BrokeragesController#create_brokerage as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FPgLuQX9yuv5kT/9OH9QOubMDhYvxcE/44ftpGmjAuep6GtlBqxc9avfEnMb2A1DzIbUSSy1jPxrO5UZOzxdow==", "brokerage"=>{"user"=>{"first_name"=>"123456", "last_name"=>"123456", "email"=>"123456", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "about"=>"ads", "phone"=>"1234567890", "email"=>"adasda"}, "commit"=>"Create Brokerage"}
Completed 500 Internal Server Error in 22ms

ActiveRecord::UnknownAttributeError (unknown attribute 'user' for Brokerage.):
  app/controllers/brokerages_controller.rb:17:in `create_brokerage'

已创建经纪,但未创建用户或经纪关系。我需要添加什么才能创建用户。我尝试将 brokerage_params 更改为:

def brokerage_params
  params.require(:brokerage).permit(:first_name, :last_name, :about, :email, :phone, :image, user_attributes: [:id, :first_name, :last_name, :email, :password, :password_confirmation])
end

并在服务器日志中得到:

Started POST "/brokerages/create_brokerage" for 127.0.0.1 at 2015-09-02 23:30:50 -0700
Processing by BrokeragesController#create_brokerage as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FPgLuQX9yuv5kT/9OH9QOubMDhYvxcE/44ftpGmjAuep6GtlBqxc9avfEnMb2A1DzIbUSSy1jPxrO5UZOzxdow==", "brokerage"=>{"user"=>{"first_name"=>"123456", "last_name"=>"123456", "email"=>"123456", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "about"=>"ads", "phone"=>"1234567890", "email"=>"adasda"}, "commit"=>"Create Brokerage"}
Unpermitted parameter: user

您可能需要:

def brokerage_params
  params.require(:brokerage).permit(:first_name, :last_name, :about, :email, :phone, :image, :user_attributes => [:id, :first_name, :last_name, :email, :password, :password_confirmation])
end

并修正你的表格:

<%= f.fields_for :users do |builder| %>
  <%= render 'users/form_elements', f: builder %>
<% end %>

请将user改为users

def create_brokerage
  @brokerage = Brokerage.new(brokerage_params)
  @brokerage.first_name = params[:brokerage][:users][:first_name]
  @brokerage.last_name = params[:brokerage][:users][:last_name]
  clean_phone
  if @brokerage.save
    redirect_to login_path, notice: 'Broker was saved.'
  else
    render 'brokerages/signup'
  end
end

private
def brokerage_params
  params.require(:brokerage).permit(:first_name, :last_name, :about, :email, :phone, :image, users_attributes: [:id, :first_name, :last_name, :email, :password, :password_confirmation])
end

更新

<%= f.fields_for :users do |builder| %>
  <%= render 'users/form_elements', f: builder %>
<% end %>

它将是 users_attributes 而不是 brokerage_params 方法中的 user_attributes。看看 https://github.com/ryanb/nested_form。 谢谢