我究竟做错了什么? [Rails, belongs_to]

What am I doing wrong? [Rails, belongs_to]

卡在嵌套表单上..

订购型号:

class Order < ActiveRecord::Base

  belongs_to :user
  accepts_nested_attributes_for :user

end

用户模式:

class User < ActiveRecord::Base

  has_many :orders, dependent: :destroy
  accepts_nested_attributes_for :orders

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

end

表单视图:

=form_for @order do |order|
    =order.fields_for :user, @order.user do |user|
        .row
            .col-md-3
                .form-group
                    =user.label :first_name, "Name"
                    =user.text_field :first_name, :class => "form-control"
            .col-md-3
                .form-group
                    =user.label :last_name, "Last name"
                    =user.text_field :last_name, :class => "form-control"
            .col-md-3
                    =user.label :email, "Email"
                    =user.text_field :email, :class => "form-control"
            .col-md-3
                    =user.label :telephone, "Phone"
                    =user.text_field :telephone, :class => "form-control"

        .row
            .col-md-4.margin-top-15
                =order.submit 'Send', :class => 'btn btn-success'

订单控制器:

class OrdersController < ApplicationController

  def new
    @order = Order.new
    if user_signed_in?
       user = current_user
    else
       user = User.new
    end
  end

  def create
    @order = Order.new order_attributes
    @order.save
  end

  private

    def order_attributes
      params.require(:order).permit(:user_id, user_attributes: [:id, :user_id, :user, :first_name, :last_name, :email, :telephone, :password, :password_confirmation])
    end

end

这就是我想要做的:

用户模型有设计。我想创建订单并分配给它 user_id。提交时它告诉我 "Unpermitted parameter: user"。订单模型创建其列,但用户模型没有任何内容。

我做错了什么?

变化:

params.require(:order).permit(:user_id, user_attributes: [:id,...

至:

params.require(:order).permit(:user_id, user: [:id,...

:user_attributes 中删除 :user。 而且我不认为 :user_id 是必要的。