【问题标题】:Simple Ruby on Rails database call succeeding but page not populating简单的 Ruby on Rails 数据库调用成功但页面未填充
【发布时间】:2016-11-09 04:10:42
【问题描述】:

我是 Ruby on Rails 的新手。尝试创建一个以流行的 MOOC 教科书Engineering Software as a Service 中的the simple "Rotten Potatoes" sample app 为模型的入门项目。书中的示例代码运行良好(duh),但是当我尝试进行一些更改时,我得到了一些奇怪的行为。我使用的是 Rails 5.0.0.1,这不是本书使用的!

我正在尝试进行简单的数据库读取并将结果显示在表格中。我生成的页面在表中有正确的行数,所以我知道有一些成功的数据库连接。即使我将更多示例数据添加到数据库并重新加载应用程序,页面也会显示新的正确表行数。但错误是,它不会用任何数据填充表格单元格。

我正在使用 MVC。我的代码:

型号,person.rb

class Person < ActiveRecord::Base
# attr_accessor :name, :email, :ID
# The above line is equally as broken as the current state

# attr_accessible :name, :email, :ID
# Something like this is what was used in the original Rotten Potatoes
# Cannot use attr_accessible in Rails 5... must use strong parameters instead?
# https://github.com/rails/strong_parameters
end

控制器,people_controller.rb

class PeopleController < ApplicationController
    def index
        @people = Person.all
    end

    private
        def person_params
          params.require(:person).permit(:name, :email, :id)
        end
end

查看,index.html.haml

%table#people
  %thead
    %tr
      %th Name
      %th E-mail address
      %th ID
      %th Edit
  %tbody
    - @people.each do |person|
      %tr
        %td= person.name
        %td= person.email
        %td= person.ID
        %td= link_to "Edit", edit_person_path(person)

实际生成的 HTML:

<table id='people'>
  <thead>
    <tr>
      <th>Name</th>
      <th>E-mail</th>
      <th>ID</th>
      <th>Edit</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td><a href="/people/1/edit">Edit</a></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td><a href="/people/2/edit">Edit</a></td>
    </tr>
    ...

所需生成的 HTML:

<table id='people'>
  <thead>
    <tr>
      <th>Name</th>
      <th>E-mail</th>
      <th>ID</th>
      <th>Edit</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John Q. Tester</td>
      <td>jqt@example.com</td>
      <td>0001</td>
      <td><a href="/people/1/edit">Edit</a></td>
    </tr>
    <tr>
      <td>Testy D. Smith</td>
      <td>tds@example.org</td>
      <td>0002</td>
      <td><a href="/people/2/edit">Edit</a></td>
    </tr>
    ...

起初我在模型中使用attr_accessible。但是我收到了一个错误,消息建议尝试attr_accessor。所以我改变了它,应用程序运行没有错误,但数据没有显示在表格中。然后我发现Rails 4 stopped supporting attr_accessible and it was replaced by strong parameters。所以我尝试使用这些,但它并没有改善结果。我不知道我是否尝试了错误的事情,或者我是否走在正确的道路上,只是做错了。

对不起,如果这不是足够的信息。请告诉我,我会尽力更新。在这一点上,我什至不知道 MCVE 与 Ruby on Rails 有什么关系。

编辑:

Rails 控制台输出:

2.3.0 :005 > @people = Person.all
  Person Load (0.3ms)  SELECT "people".* FROM "people"
 => #<ActiveRecord::Relation [#<Person id: 1, name: nil, ID: nil, email: nil, created_at: "2016-11-08 08:14:17", updated_at: "2016-11-08 08:14:17">, #<Person id: 2, name: nil, ID: nil, email: nil, created_at: "2016-11-08 08:14:17", updated_at: "2016-11-08 08:14:17">, ...]> 

【问题讨论】:

  • 你的@people = Person.all是返回集合对象吗?在 Rails 控制台中检查它
  • @sa77 抱歉耽搁了,我不得不去了解 Rails 控制台是什么...输出非常有趣,我将其编辑到帖子中以保留格式,但简短版本是所有数据为零...不确定数据库是否播种错误或查询本身是否以某种方式损坏,但我倾向于播种...
  • 这是你的问题.. DB 相关.. 你的 rails 代码库看起来不错
  • 显示您的 database.yml 文件并共享您的终端

标签: ruby-on-rails


【解决方案1】:

你在正确的道路上 attr_accessible 已从 rails 4 中删除,现在它使用强参数。

class PeopleController < ApplicationController
    def index
        @people = Person.all
    end

    private
        def person_params

          params.require(:person).permit(:name, :email,:ID)
        end
end

Create some records from rails console

People.create(name: "anik",email: "abc@gmail.com",ID: 2)

您没有在数据库中插入任何记录,这就是它现在显示任何记录的原因。首先您需要通过 rails 控制台插入 在数据库中插入记录时,请确保您使用的列的数据类型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多