我是Ruby on Rails的新手。尝试创建一个以流行的MOOC教科书the simple "Rotten Potatoes" sample app中的Engineering Software as a Service为模型的初学者项目。书中的示例代码工作正常(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
Controller,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。所以我尝试使用它们,但它没有改善结果。我不知道我是否尝试过错误,或者我是否在正确的道路上并且做错了。
很抱歉,如果这还不够。请告诉我,我会尽力更新。在这一点上,我不知道什么与Ruby on Rails的MCVE有关。
编辑:
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">, ...]>
答案 0 :(得分:0)
Well you are on a right path attr_accessible has been removed from rails 4 and now it uses strong parameters .
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)
You Don't have any records inserted in database that is why it is now showing any records.Firstly you need to insert by rails console Just Make sure that what datatype you have used for column while inserting records in database