レシピのウェブサイトを作成しようとしていて、データを配列としてデータベースに保存するためにカール要求を行うといくつかの問題が発生します。バックエンドにはレールを、データベースには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"]}
だから、誰も私が問題だ何を聞かせだろうか?それを修正する方法は?ありがとうございました。
あなたが直面している問題は何ですか? –