Исправлено : если после последнего элемента в массиве нет запятой, он будет постоянно удалять последний элемент, поэтому это не будет работать:
["one", "two", "three"]
Но это будет:
["one", "two", "three",]
У меня есть атрибуты массива, называемые «технологиями» и «функциями» для моей модели Project. Когда я создаю модель, правильная длина массива передается в параметрах, как и ожидалось, однако, когда параметры установлены для проекта, последний элемент был удален. Вот что передается в параметрах:
{"authenticity_token"=>"",
"project"=>
{"name"=>"test", "color"=>"", "url"=>"", "github"=>"", "technologies"=>"[\"one\", \"two\", \"three\"]", "description"=>"", "difficulties"=>"", "solution"=>"", "features"=>"one, two, three"},
"commit"=>"Create Project"}
Как выглядит созданный проект:
=> #<Project id: nil, name: "test", color: "", url: "", github: "", technologies: ["one", " two"], description: "", difficulties: "", solution: "", features: ["ne", " two"], created_at: nil, updated_at: nil, slug: nil>
Сильные параметры:
def strong_params
params.require(:project).permit(:name, :color, :url, :github, :technologies, :description, :difficulties, :solution, :features, photos: [])
end
Метод create в контроллер Projects:
def create
project = Project.new(strong_params)
if project.save
redirect_to project_path(project)
else
render :new
end
end
Вот схема для таблицы Project:
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :name
t.string :color
t.string :url
t.string :github
t.text :technologies, array: true, default: []
t.text :description
t.text :difficulties
t.text :solution
t.text :features, array: true, default: []
t.timestamps
end
end
end
Есть идеи, почему он это делает?
Редактировать:
Запустил тест в консоли и браузере с массивом: ["a", "b", "c", "d", "e"]. Через консоль все работает как положено, однако в браузере удаляет последний элемент:
[1] pry(main)> Project.create!(name: "test2", technologies: ["a", "b", "c", "d", "e"], features: ["a", "b", "c", "d", "e"])
(0.2ms) BEGIN
Project Create (7.8ms) INSERT INTO "projects" ("name", "technologies", "features", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "test2"], ["technologies", "{a,b,c,d,e}"], ["features", "{a,b,c,d,e}"], ["created_at", "2020-04-25 14:03:17.069772"], ["updated_at", "2020-04-25 14:03:17.069772"]]
(1.7ms) COMMIT
=> #<Project:0x00007ff48760e900
id: 9,
name: "test2",
color: nil,
url: nil,
github: nil,
technologies: ["a", "b", "c", "d", "e"],
description: nil,
difficulties: nil,
solution: nil,
features: ["a", "b", "c", "d", "e"],
created_at: Sat, 25 Apr 2020 14:03:17 UTC +00:00,
updated_at: Sat, 25 Apr 2020 14:03:17 UTC +00:00>
[2] pry(main)> Project.last
Project Load (0.4ms) SELECT "projects".* FROM "projects" ORDER BY "projects"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Project:0x00007ff48765e4c8
id: 9,
name: "test2",
color: nil,
url: nil,
github: nil,
technologies: ["a", "b", "c", "d", "e"],
description: nil,
difficulties: nil,
solution: nil,
features: ["a", "b", "c", "d", "e"],
created_at: Sat, 25 Apr 2020 14:03:17 UTC +00:00,
updated_at: Sat, 25 Apr 2020 14:03:17 UTC +00:00>
[3] pry(main)> reload!
Reloading...
=> true
[4] pry(main)> Project.last
Project Load (0.1ms) SELECT "projects".* FROM "projects" ORDER BY "projects"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Project:0x00007ff4880ffda0
id: 10,
name: "test3",
color: "",
url: "",
github: "",
technologies: ["a", " b", " c", " d"],
description: "",
difficulties: "",
solution: "",
features: ["a", " b", " c", " d"],
created_at: Sat, 25 Apr 2020 14:04:38 UTC +00:00,
updated_at: Sat, 25 Apr 2020 14:04:38 UTC +00:00>