Ruby on Rails未定义的方法`每个&#39; for#<string:0x7efa908>错误

时间:2018-06-05 04:57:02

标签: ruby-on-rails

我试图通过html中的字符串数组,但得到&#34;未定义的方法`每个&#39;为#&#34;错误。 Error image

这是Planet class

的类定义
 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>

以下是我迁移的方式

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."])

首先,我在类定义中尝试了默认:[],但它失败了,所以我将其更改为&#39; {}&#39;。如果有人知道如何处理这个问题,请告诉我。

谢谢。

编辑:

在我尝试@ planet.description.lines.each后,输出更改为此。 Current Output

哪个列表在同一行,并且还包括[],它应该是数组的外部容器

更新

现在我将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

我的种子.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

我的星球课

class Planet < ApplicationRecord
    serialize :description, Array
end

1 个答案:

答案 0 :(得分:0)

Rails provides a way to create array type column, but ActiveRecord only supports PostgreSQL array columns, it won't work with mysql2 db so, i came across the same situation, and i did it by serialize column (By doing this you are independent of database dependency.)

1- Create a column of type text (or db:rollback if its last migration)

t.text :description

2- In Planet model

serialize :description, Array

3 -

p1 = Planet.new(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

4- Now you can go through each description , and it will treat as array instead of string

<%unless @planet.blank?%>
  <h1><%= @planet.name %></h1>
  <%= image_tag @planet.image %>
  <ul>
      <%unless @planet.description.blank?%>
        <% @planet.description.each do |x| %>
        <li><%= x %></li>
        <% end %>
      <%end%>
  </ul>
<%end%>