我想制作一个配方网站,当我尝试执行一个curl请求以将数据作为数组保存到我的数据库时遇到一些问题。我为我的后端使用了rails,而我的数据库使用了psql。无法通过导轨将数组保存到数据库中
迁移
class CreateRecipes < ActiveRecord::Migration
def change
create_table :recipes do |t|
t.text :instruction, array: true, default: []
end
end
end
模型
class Recipe < ActiveRecord::Base
serialize :instruction,Array
end
控制器
def create
@recipe = Recipe.new(recipe_params)
**_binding.pry_**
current_user.recipes << @recipe
if @recipe.save
render json: @recipe, status: :created, location: @recipe
else
render json: @recipe.errors, status: :unprocessable_entity
end
end
def recipe_params
params.require(:recipes)
.permit(:name, :category, instruction: [])
end
卷曲请求
curl --include --request POST http://localhost:3000/recipes \
--header "Authorization: Token token=........" \
--header "Content-Type: application/json" \
--data '{
"recipes": {
"name": "an example recipe",
"category": "fry",
"instruction": ["do it", "ignore it"]
}
}'
后,我用撬测试,
pry(#<RecipesController>)> @recipe
=> #<Recipe:0x007fa0aeafa770 id: nil, name: "an example recipe", category: "fry", instruction: [], user_id: nil, created_at: nil, updated_at: nil>`
pry(#<RecipesController>)> recipe_params
=> {"name"=>"an example recipe", "category"=>"fry", "instruction"=>["do it", "ignore it"]}
所以任何人都可以让我知道了什么问题?以及如何解决它?谢谢。
你面临的问题是什么? –