簡體   English   中英

簡單的Ruby on Rails數據庫調用成功,但頁面未填充

[英]Simple Ruby on Rails database call succeeding but page not populating

我是Ruby on Rails的新手。 嘗試根據流行的MOOC教科書“ 工程軟件即服務 ”中的簡單“爛土豆”示例應用程序創建一個原型項目。 本書中的示例代碼運行良好(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停止支持attr_accessible ,並由強參數代替 因此,我嘗試使用這些方法,但是並沒有改善結果。 我不知道我是否嘗試了錯誤的事情,或者我是否走對了,只是做錯了。

很抱歉,如果這還不夠。 請告訴我,我會盡力更新。 在這一點上,我什至不知道與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">, ...]> 

好吧,您在正確的路徑上attr_accessible已從滑軌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 console進行插入, 只需確保在數據庫中插入記錄時已為列使用了哪種數據類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM