找不到没有ID的模型(带有强参数的rails4)

时间:2013-09-04 10:36:14

标签: ruby-on-rails has-many belongs-to strong-parameters

我有2个模型用户和订单

在用户模型中我有以下

class User < ActiveRecord::Base
 include ActiveModel::ForbiddenAttributesProtection

 has_many orders
end

在订单模型中,我有

class Order < ActiveRecord::Base
 include ActiveModel::ForbiddenAttributesProtection

 belongs_to user
end

在订单控制器中我有

class OrdersController < ApplicationController
 before_action :set_order, only: [:show, :edit, :update, :destroy]

 def create   
@user = User.find(params[:user_id])     
@order = @user.orders.build(order_params)

respond_to do |format|
  if @order.save
    format.html { redirect_to @order, notice: 'Order was successfully created.' }
    format.json { render action: 'show', status: :created, location: @order }
  else
    format.html { render action: 'new' }
    format.json { render json: @orders.errors, status: :unprocessable_entity }
  end
end
end

  private

  def order_params    
    params[:order].permit( :order_title, :propose_amount, 
     :terms,:users_attributes =>  [:user_id]) 
  end

end

创建订单时,我不断收到错误

    Couldn't find user without an ID

并且以下行被突出显示为错误行

    @user = User.find(params[:user_id]) 

有谁知道哪里出错了?

0 个答案:

没有答案