Mysql 不保存用户id的嵌套模型的新建和创建操作

Mysql 不保存用户id的嵌套模型的新建和创建操作,mysql,ruby-on-rails,ruby,ruby-on-rails-4,stack-overflow,Mysql,Ruby On Rails,Ruby,Ruby On Rails 4,Stack Overflow,好的,我有三个模型…一个用户,一个集合,和一个设计 我的模型= 用户模型 `has_many :collections has_many :designs, :through => :collections` 集合模型 `belongs_to :user has_many: designs` 设计模型 `belongs_to :user belongs_to :collection` 好的,当我尝试创建一个集合时,一切都很好。所有参数都会保存到数据库中,包括使用(当前用户)

好的,我有三个模型…一个用户,一个集合,和一个设计

我的模型=

用户模型

 `has_many :collections
  has_many :designs, :through => :collections`
集合模型

`belongs_to :user
 has_many: designs`
设计模型

`belongs_to :user
 belongs_to :collection`
好的,当我尝试创建一个集合时,一切都很好。所有参数都会保存到数据库中,包括使用(当前用户)与其关联的用户id

当我尝试创建一个设计(属于一个集合)时,我的问题就出现了。当我创建一个新的设计时,用户id没有被存储

这是我为新方法和创建方法设计的控制器

设计控制器:

`def new
   if signed_in? && current_user == @collection.user
     @user = current_user
     @collection = @user.collections.find(params[:collection_id])
     @design = @collection.designs.new
   else
     flash[:error] = "That's not your collection"
     redirect_to root_url
  end
end`

'def create
   @collection = current_user.collections.find(params[:collection_id])
   @design = @collection.designs.new(design_params)

   respond_to do |format|
     if @design.save
       format.html { redirect_to collection_designs_path(@collection), notice: 'Design was successfully created.' }
      format.json { redirect_to collection_designs_path(@collection) }
    else
      format.html { render 'designs/new' }
      format.json { render json: @design.errors, status: :unprocessable_entity }
    end
  end
end`
这是我使用的表单(减去字段)

`{:multipart=>true,:class=>“auth”}do | f |%>
`

只是澄清一下,我可以提交表单,表单可以工作,设计创建时附带集合id,但不幸的是,用户id没有与之关联…

您没有将任何用户对象与设计关联。试试这是设计控制器。 在行动上,

设计控制器

def create
   @collection = current_user.collections.find(params[:collection_id])
   @design = @collection.designs.new(design_params)
   @design.user = current_user

   respond_to do |format|
     if @design.save
       format.html { redirect_to collection_designs_path(@collection), notice: 'Design was successfully created.' }
      format.json { redirect_to collection_designs_path(@collection) }
    else
      format.html { render 'designs/new' }
      format.json { render json: @design.errors, status: :unprocessable_entity }
    end
  end
end`

非常感谢你!我不敢相信我漏掉了
def create
   @collection = current_user.collections.find(params[:collection_id])
   @design = @collection.designs.new(design_params)
   @design.user = current_user

   respond_to do |format|
     if @design.save
       format.html { redirect_to collection_designs_path(@collection), notice: 'Design was successfully created.' }
      format.json { redirect_to collection_designs_path(@collection) }
    else
      format.html { render 'designs/new' }
      format.json { render json: @design.errors, status: :unprocessable_entity }
    end
  end
end`