Ruby on Rails的嵌套属性不能保存到数据库中吗? [英] Ruby on Rails nested attributes not saving into database?

查看:70
本文介绍了Ruby on Rails的嵌套属性不能保存到数据库中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在"Todo列表"中创建项目列表,但是,我不确定是否使用嵌套属性正确执行了此操作.我认为使用嵌套属性是正确的尝试,因为将会有大量的项目列表,并且它将与基于id的正确"Todo列表"相关联.

I'm trying to create a list of items within a "Todo list", however, I'm not sure if I'm doing this correctly with nested attributes. I think using a nested attribute is the right attempt because there's going to be a large list of items, and it will be associated with the correct "Todo list" based on ids.

填充记录时表 可能 的外观示例

Example of what the tables might look like when records are populated

Todo表

id         list       
1          grocery shopping
2          health insurance

项目表

id         todo_id        name           
1          1              buy milk
2          1              buy cereal
3          2              Blue Shield
4          2              Healthnet
5          1              buy cherries


尽管,通过下面的尝试,我的应用程序并未将任何数据保存到Item数据库中.


Although, with my attempt below, my application is not saving any of the data into the Item database.

Todo控制器

class TodoController < ApplicationController
  def new
    @todo = Todo.new
    @todo.items.build
  end
end

Todo模型

class Todo < ActiveRecord::Base
  belongs_to :user
  has_many :items
  accepts_nested_attributes_for :items
end

项目模型

class Item < ActiveRecord::Base
    belongs_to :todo
end

Todo View

<%= simple_form_for(@todo) do |f| %>
  <%= f.input :list %>

  <%= f.simple_fields_for :items do |g| %>
    <%= g.input :name %>
  <% end%>

  <%= f.button :submit %>
<% end %>

我能够在视图中显示name字段,但是当我保存它时,它不会保存到数据库中,但是,我能够将列表保存到数据库中,然后当我尝试编辑记录时,name字段不再显示以进行编辑.

I was able to have the name field show up in my view, but when I save it, it doesn't save into the database, however, I'm able to save the list into the database, and then when I try to edit the record, the name field doesn't show up anymore to be able to edit.

显示创建方法

这是我当前在Todo Controller中创建方法

This is my current Create Method in Todo Controller

def create
  @todo = Todo.new(todo_params)

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

不确定Edit是否需要一些东西,但是我只能从生成Todo支架中获得

Not sure if Edit needs to have something, but I only have this from generating a scaffold of Todo

def edit
end


编辑2 显示todo_params


EDIT 2 show todo_params

def todo_params
  params.require(:todo).permit(:user_id, :list)
end

推荐答案

您必须将嵌套参数添加到强参数中

You must add the nested params to your strong params

def todo_params
  params.require(:todo).permit(:user_id, :list, items_attributes: [:id, :text, ...])
end

注意有关todo_id:

Note about todo_id :

您不需要在items_attributes列表中添加:todo_id,因为您已经将TODO作为上下文.

You don't need to add :todo_id in items_attributes list, because you already have the TODO as context.

@todo = Todo.new(todo_params)

在上面的代码中,您的todo_params将包含一些链接到@todo的item_attributes.即,它类似于这样做

In the above code, your todo_params will contain some item_attributes linked to @todo. ie, it's similar to doing

@todo.items.build

它将已经创建一个具有todo_id对应于@todo.id

It will already create an item with a todo_id corresponding to @todo.id

这篇关于Ruby on Rails的嵌套属性不能保存到数据库中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆