无法保存嵌套属性

时间:2015-09-03 06:32:58

标签: ruby-on-rails ruby strong-parameters

以下是表单视图:

<% 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

3 个答案:

答案 0 :(得分:1)

请将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 %>

答案 1 :(得分:1)

在brokerage_params方法中,users_attributes不是user_attributes。看看https://github.com/ryanb/nested_form。 感谢

答案 2 :(得分:-1)

您可能需要:

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 %>