【发布时间】:2017-10-19 19:10:02
【问题描述】:
我是新手,需要一些帮助。我有两个模型(通过脚手架生成)。类别和产品。
class Category
include Mongoid::Document
field :name, type: String
has_many :products
accepts_nested_attributes_for :products
end
class Product
include Mongoid::Document
include Mongoid::Paperclip
include Mongoid::Timestamps::Created
field :name, type: String
field :description, type: String
field :prize ,type: Integer
field :category_id
field :user_id
has_mongoid_attached_file :avatar,
:styles => {
:thumb => "150x150#",
:small => "150x150>",
:medium => "550x550#{}" }
belongs_to :user
belongs_to :category
end
我的类别控制器
# POST /categories.json
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
def category_params
params.require(:category).permit(:name,product_attributes: [ :name, :description,:prize ,:category_id])
end
end
我的json格式
{
"name":"Mens Clothing",
"products_attributes": [
{
"name":"Denim jeans",
"Description": "test test test"
"prize":1000,
"user_id":1,
}
]
}
现在我的问题是,当我尝试通过邮递员访问 localhost:3000/categories 时,值没有得到保存。另外我不知道我的 json 格式是否正确,或者我的类别控制器代码是否正确。我是新来的,并试图弄清楚如何以 json 格式保存 rails.iam 中 has_many 关系的数据丢失参数错误
【问题讨论】:
标签: ruby-on-rails json ruby postman