Я пытался просмотреть строковый массив в html, но получил ошибку "undefined method` each 'for # ". Изображение ошибки
Вот определение класса для класса Планета
class CreatePlanets < ActiveRecord::Migration[5.0]
def change
create_table :planets do |t|
t.string :name, null: false
t.string :image, null: false
t.string :description, array: true, default: '{}'
t.timestamps
end
end
end
Вот HTML-страница
<!DOCTYPE html>
<html>
<head>
<title><%= @planet.name %></title>
</head>
<body>
<h1><%= @planet.name %></h1>
<%= image_tag @planet.image %>
<ul>
<% @planet.description.each do |x| %>
<li><%= x %></li>
<% end %>
</ul>
</body>
</html>
Вот как я мигрировалit
p1 = Planet.create(name: "Sun", image: "/../assets/images/sun.jpg", description: ["The center of the solar system and the only star in solar system.",
"Due to its huge mass, other planets in solar system form radiant power between sun and itself, maintaining the rotation around sun",
"The surface temperature is said to be about 6000 degree celcius."])
Сначала я попробовал default: [] в определении класса, но это не удалось, поэтому я изменил его на '{}'.Если кто-то знает, как решить эту проблему, пожалуйста, дайте мне знать.
Спасибо.
Отредактировано:
После того, как я попробовал @ planet.description.lines.each, вывод изменится на этот. Токовый выход
Имеет списки в одной строке и также содержит [], который должен быть внешним контейнером массива
Обновление:
Теперь я изменил класс CreatePlanets на
class CreatePlanets < ActiveRecord::Migration[5.0]
def change
create_table :planets do |t|
t.string :name, null: false
t.string :image, null: false
t.text :description
t.timestamps
end
end
end
My seed.rb
p1 = Planet.create(name: "Sun", image: "/../assets/images/sun.jpg")
p1.description.push("The center of the solar system and the only star in solar system.",
"Due to its huge mass, other planets in solar system form radiant power between sun and itself, maintaining the rotation around sun",
"The surface temperature is said to be about 6000 degree celcius.")
p1.save
Класс My Planet
class Planet < ApplicationRecord
serialize :description, Array
end